Question

I have a parsing function, It parse a file and take a long time. I want to show a progress bar while the function parsing the file. I add toolstripprogressbar and set its properties. However I dont know how they are run synchronously.

I want to do something like this:

// progressBar start progress
parseFile();
// progressBar finish progress

I searched about it, for loop and thread are used for this, but they are not helpful for me. How can I do this?

Was it helpful?

Solution

You really need to run the long running operation in a background thread, which can periodically report back to the GUI thread about how it's doing (and, in the process, update your progress bar).

I'd urge you to read about Background Worker, here http://www.dotnetperls.com/backgroundworker

MSDN Page here : http://msdn.microsoft.com/en-us/library/8xs8549b.aspx

You can also see a very similar SO question here: C# Winform ProgressBar and BackgroundWorker

OTHER TIPS

If you want to show the progress then you have to somehow divide your long-process on either stages or processed data intervals - and increase progressbar position after eash stage/interval have been processed.

Example for 3 stages: Set ProgressBar maximum value to 3. Stage 1) Opening and reading file to memory - set progressBar.Value to 1 after finishing this Stage 2) Parsing file - set progressBar.Value to 2 after that Stage 3) Additional processing ? - after that set progressBar.Value to 3 = Max = process completed.

Example for file size: Let's imagine you can measure what part of file you've parsed so far, in bytes. Then first you set progressBar.Max to file size in bytes. Then after every processed block of bytes from file you should set progressBar.Value to processed bytes count.

Second approach is usually more precise and progressBar runs much more smoothly, but it's not always possible to use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top