[Mono-list] Label stops updating during System.Timers.Timer callbacks

Antonio Santana antjensavwork@hotmail.com
Wed, 22 Sep 2004 08:15:41 -0500


<html><div style='background-color:'><DIV class=RTE>
<P>Ahhh.&nbsp; Very true.&nbsp; I have done this in a Winforms environment and it will allow the Timer thread to attach to the same thread the Winform is using.&nbsp; I guess you can't do that with a Gtk window.&nbsp; That might be something to specifically ask the Mono team.</P>
<P>Does anyone from the Mono team know why the below will not work?</P>
<P>Thanks!</P>
<P>Antonio<BR><BR></P></DIV>
<DIV></DIV>&gt;From: Jimmy Do &lt;crispyleaves@gmail.com&gt; 
<DIV></DIV>&gt;Reply-To: Jimmy Do &lt;crispyleaves@gmail.com&gt; 
<DIV></DIV>&gt;To: Antonio Santana &lt;antjensavwork@hotmail.com&gt; 
<DIV></DIV>&gt;Subject: Re: [Mono-list] Label stops updating during System.Timers.Timer callbacks 
<DIV></DIV>&gt;Date: Tue, 21 Sep 2004 16:20:45 -0700 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Hmm.... 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;timer.SynchronizingObject = this; 
<DIV></DIV>&gt;gives a compile-time error: 
<DIV></DIV>&gt;"Cannot convert implicitly from `TimerWindow' to 
<DIV></DIV>&gt;`System.ComponentModel.ISynchronizeInvoke'(CS0029)" 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;'this' refers to a Gtk.Window, so maybe that's why it doesn't work? 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Jimmy 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;----- Original Message ----- 
<DIV></DIV>&gt;From: Antonio Santana &lt;antjensavwork@hotmail.com&gt; 
<DIV></DIV>&gt;Date: Tue, 21 Sep 2004 14:19:10 -0500 
<DIV></DIV>&gt;Subject: RE: [Mono-list] Label stops updating during 
<DIV></DIV>&gt;System.Timers.Timer callbacks 
<DIV></DIV>&gt;To: crispyleaves@gmail.com, mono-list@lists.ximian.com 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;You might try the following: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; myCounter = 0; 
<DIV></DIV>&gt; &gt; 
<DIV></DIV>&gt; &gt; Timer timer = new Timer(); 
<DIV></DIV>&gt; &gt; timer.Interval = 500; 
<DIV></DIV>&gt; &gt; timer.Elapsed += new ElapsedEventHandler(OnTimeElapsed); 
<DIV></DIV>&gt;//This sets the SynchronizingObject property to 
<DIV></DIV>&gt;//&nbsp;&nbsp; synchronize with the current window or form object 
<DIV></DIV>&gt;//Setting the below property should force the Timer to use the same 
<DIV></DIV>&gt;thread as the UI 
<DIV></DIV>&gt;//&nbsp;&nbsp; so they don't get out of synch; otherwise, it would use it's own 
<DIV></DIV>&gt;worker thread. 
<DIV></DIV>&gt;timer.SynchronizingObject = this; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; timer.Start(); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Another thing you might try doing in addition to the above: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; private void OnTimeElapsed(object o, ElapsedEventArgs args) 
<DIV></DIV>&gt; &gt; { 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; myCounter++; REMOVE THIS LINE 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; myCountLabel.Text = Convert.ToString(Convert.ToInt32(myCounter.Text)+1)); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;//This would merely be so you are not storing the value in the label 
<DIV></DIV>&gt;object as well as the myCounter variable 
<DIV></DIV>&gt; &gt; 
<DIV></DIV>&gt; &gt; Console.WriteLine(myCounter.ToString()); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; &gt; } 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;I hope this helps.&nbsp;&nbsp;Let me know if this doens't improve anything. 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Thanks! 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Antonio 
<DIV></DIV></div></html>