Click here to Skip to main content
16,016,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there im trying to display the progress percentage currently it looks like this

Downloaded Percentage: 1%
Downloaded Percentage: 2%
Downloaded Percentage: 3%
Downloaded Percentage: 4%

which is wrong i want it to update with out one after another my code is below can someone please point out where im going wrong.

C#
var videoDownloader = new VideoDownloader(video,
                Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
                RemoveIllegalPathCharacters(video.Title) + video.VideoExtension));

            for (int i = 0; i < videoDownloader.SavePath.Length; i++)
            {
                // Register the ProgressChanged event and print the current progress
                videoDownloader.DownloadProgressChanged += (sender, args) => Console.Write(string.Format("Downloaded Percentage: {0}", args.ProgressPercentage));
            }
Posted
Comments
Tomas Takac 13-Oct-15 8:36am    
Why are you registering the DownloadProgressChanged handler in a loop for each character in the SavePath? This doesn't make sense.
Member 11838038 13-Oct-15 9:03am    
Sorry man still learning :)

You need to save the cursor position before you start, and set it[^] before you write the message:
C#
int left = Console.CursorLeft;
int top = Console.CursorTop;

videoDownloader.DownloadProgressChanged += (sender, args) => 
{
    Console.SetCursorPosition(left, top);
    Console.Write("Downloaded Percentage: {0}", args.ProgressPercentage);
};

(There's no need to call string.Format when writing to the console; both Write and WriteLine have overloads which accept a composite format string and arguments.)
 
Share this answer
 
Comments
Member 11838038 13-Oct-15 8:55am    
Thanks so much so i didnt need the for(){} ?
Richard Deeming 13-Oct-15 8:58am    
As Tomas said, the for loop didn't make much sense. You should only need to attach the event handler once.
Member 11838038 13-Oct-15 9:03am    
Yes you correct thank you much for help
There's a trick to this.

Console.Write("Something" + "\r")
will return the cursor back to the beginning of the current line.

So you would use
Console.Write("Downloaded Percentage: {0, -3}\r", args.ProgressPercentage);

As the line is overwritten it's important to ensure that the new text is at least as long as the old stuff. Hence the use of the 3 character left aligned field in the example.

An alternative is to just end the line with a few spaces, e.g.
Console.Write("Downloaded Percentage: {0}% \r", args.ProgressPercentage);
3 spaces will be ok as long as the new line is no more than 3 chars shorter than the original.

Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900