Timer message queue
-
I'm using a System.Timers.Timer to send some data to a serial port every so often. Problem is, if the main thread gets tied up, with internal processing, or hitting a breakpoint for example. A long queue of events gets queued up, then once the app is back in business, all of the messages get handled almost simultaniously. I've tried to come up with a work around, but I'm stumped, any suggestions. Thanks, sam
-
I'm using a System.Timers.Timer to send some data to a serial port every so often. Problem is, if the main thread gets tied up, with internal processing, or hitting a breakpoint for example. A long queue of events gets queued up, then once the app is back in business, all of the messages get handled almost simultaniously. I've tried to come up with a work around, but I'm stumped, any suggestions. Thanks, sam
do not use a timer but use a thread. Timers use the WM_TIMER message internally. If the message queue dispatching is deleayed (i.e. by some other messages that take a long time to process) a lot of WM_TIMER messages will be queued and then be dispatched all at once. a thread function could look like this:
private ManualeResetEvent m_Done = new ManualeResetEvent(false); private int m_Delay = 1000; // time to wait between each send public void SendToPort() { while (true) { this.m_Port.Send(new byte[1]{1}); // wait m_Delay ms if we should abort if (m_Done.WaitOne(this.Delay,false) ) break; } }
use the m_Done event to abort the loop. /cadi
-
do not use a timer but use a thread. Timers use the WM_TIMER message internally. If the message queue dispatching is deleayed (i.e. by some other messages that take a long time to process) a lot of WM_TIMER messages will be queued and then be dispatched all at once. a thread function could look like this:
private ManualeResetEvent m_Done = new ManualeResetEvent(false); private int m_Delay = 1000; // time to wait between each send public void SendToPort() { while (true) { this.m_Port.Send(new byte[1]{1}); // wait m_Delay ms if we should abort if (m_Done.WaitOne(this.Delay,false) ) break; } }
use the m_Done event to abort the loop. /cadi
The OP is using
System.Timers.Timer
which does not depend on the application running a message pump. But yeah, your idea is right, a thread will solve the problem. Regards Senthil _____________________________ My Blog | My Articles | WinMacro