Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Timer message queue

Timer message queue

Scheduled Pinned Locked Moved C#
data-structuresdebuggingbusinesshelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sammyh
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • S sammyh

      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

      C Offline
      C Offline
      Carsten Zeumer
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • C Carsten Zeumer

        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

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups