System.Timer, progress bar and threading

dinoboy

Newcomer
Joined
Oct 11, 2003
Messages
24
Location
Estonia
I am developing an application which makes the search request via the web service. Meanwhile the progress bar control is displayed to the user.

Now, I am using a timer to update the progress bar.
As far as I know, the System.Timer should start in the new thread... and here I am having a problem...

Start the timer:
Code:
this.timer.Interval = 100;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.cbProgressBar_update);
this.timer.Start();

And when timer elapsed, I want to update my progress bar.
The progress bar owner is the main UI thread, but the event is raised in the timer thread. So I can't call in cpProgressBar_update method just this.progressBar.PerformStep(). Am I right?

Should I use Invoke() methods in the raised event? I have tried different solutions I have thought about, but the results weren't what I had expected...
 
Strictly speaking you should always use Invoke when calling methods of controls created on another thread. I believe that this is actually enforced in .NET 2.0. Also, I've never used a Timer other than the WinForms version but in a multi-threaded environment I'd say that a System.Threading.Timer might be the way to go. No guarantees though, just a thought.
 
Actually I don't start any new threads. Simply Asynchronous request is made and while it is making the request I want to display to the user a progress bar. And for updating it, I use System.Timers.Timer class, whcich automatically starts in a new thread.
 
Finally I found what was wrong....
The problem wasn't related with the threading. It was caused by the lenght of the progress bar and it's values. When I assigned the value to 0, then it still showed one bar.
 
Back
Top