c# - System.Threading.Timer's Dispose method does not work with ManualResetEventSlim? -


i have following code sample console app simulate windows service.

class program {     private timer timer;     private object syncroot = new object();     private bool stopsignalled = false;     private manualreseteventslim mre = new manualreseteventslim(false);     static void main(string[] args)     {         program p = new program();         p.start();         console.readline();         p.stop();         console.writeline("stopped at:{0:g}", datetime.now);     }      private void stop()     {         stopsignalled = true;         mre.wait();                  }     private void timercallback(object state)     {         lock (syncroot)         {             if (!stopsignalled)             {                  console.writeline("callback invoked  at:{0:g}",datetime.now);              }             else             {                 timer.dispose(mre.waithandle);                 console.writeline("timer disposed at:{0:g}", datetime.now);             }         }      }      private void start()     {         console.writeline("started at:{0:g}",datetime.now);         timer = new timer(timercallback,null,timespan.zero,timespan.fromseconds(1));      } } 

i surprised dispose overload accepts waithandle never signals handle when using manualreseteventslim. if change code use manualresetevent instead so, handle indeed signalled.

    class program {     private timer timer;     private object syncroot = new object();     private bool stopsignalled = false;     private manualresetevent mre = new manualresetevent(false);     static void main(string[] args)     {         program p = new program();         p.start();         console.readline();         p.stop();         console.writeline("stopped at:{0:g}", datetime.now);     }      private void stop()     {         stopsignalled = true;         mre.waitone();                  }     private void timercallback(object state)     {         lock (syncroot)         {             if (!stopsignalled)             {                  console.writeline("callback invoked  at:{0:g}",datetime.now);              }             else             {                 timer.dispose(mre);                 console.writeline("timer disposed at:{0:g}", datetime.now);             }         }      }      private void start()     {         console.writeline("started at:{0:g}",datetime.now);         timer = new timer(timercallback,null,timespan.zero,timespan.fromseconds(1));      } } 

what gives!


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -