System Timer Puzzle

G

Guest

I'm building a WinForms app that is a companion to a Pocket PC app. The
WinForms app initiates a separate thread that calls a Notification window.
This window resides in the lower right corner of the screen and keeps the
user informed about what is happening during data transfer to/from the Pocket
PC.

Everything has been working fine with the Notification window until now.
Here's the problem:

I'm using RAPI to copy the required CAB files to the Pocket PC. Some of
them are quite large, up to 2.5MB in size. During the copying, I wanted to
keep the user informed about the progress. So I instaniated a System Timer
in the Notification form that executes an event every second. One of the
thing the Interval Handler of this timer does is call the RAPI object to find
out the current filesize on the Pocket PC (ie. how many bytes have been
copied so far).

When writing this information to the Debug window, it seems to work
basically okay. But when I try to display it in a label on the Notification
form, it just does not. I tried adding "this.Refresh()" and
"Application.DoEvents()" after setting the label but this did not resolve the
problem.

Any idea why not and how I can solve the problem?
 
G

Guest

Hi Robert,
the System.Timer.Timer executes the code from a thread inside the
ThreadPool. You should only ever change a controls property using the thread
that created the control, this is not happening in your case, which could be
causing your problem.

If you want to get around this then you can either use a
System.Windows.Forms.Timer which executes inside the main UI thread - but can
be problematic if you are doing lots of processing, or you can stick with the
Timers.Timer and call Invoke or BeginInvoke on the user control, passing in a
delegate to be executed, thiswill then be executed by the thread that created
the control.

For a more detailed explanation see:
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

Hope that helps
Mark R Dawson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top