المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : DoEvents() and System.Threading.Timer



C# Programming
09-08-2010, 02:21 AM
Hi everyone,

I am trying to update some UI periodically through the use of Application.DoEvents() in response to a timer callback. However, I am having a bit of trouble getting it to work.

So, I have a dialog box that is created in the main thread. I have a long running method in a third party control and all I want to do is make sure that the dialog box stays responsive to mouse movements and can respond to move events etc. I know I should be calling the long running function in a background thread but due to the idiosyncracies of the software design and third party tools, I am unable to do so.

So what I thought I would do is create a Threading.Timer object and have it send periodic events to my dialog box and ask the message queue to process the messages.

This scenario is as follows:
m_timer = new System.Threading.Timer(new TimerCallback(TimeElapsedHandler),
null, System.Threading.Timeout.Infinite, 100);
m_timer.Change(0, 100); // Start timer

The timer handler is as follows:

private void TimeElapsedHandler(object state)
{
System.Windows.Forms.Application.DoEvents();
}

In the debug mode, I can see this event getting called during the processing. However, the DoEvents() call is not really updating the dialog box as it should.

Now, the strange thing is that when I add some Application.DoEvents() in the callbacks that I get from the third party call, the update is as I would expect it (I cannot rely on these updates to come frequently enough, hence I need a regular periodic solution).

The puzzling thing is that the timer and the dialog box is created on the main thread. So, I am puzzled as to why my update is not happening.

I would appreciate any thoughts or input you might have on this.

Best wishes,

Keith