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

Antonio Santana antjensavwork@hotmail.com
Tue, 21 Sep 2004 14:19:10 -0500


<html><div style='background-color:'><DIV class=RTE>
<P>You might try the following:</P>
<P>&gt; myCounter = 0; </P></DIV>
<DIV></DIV>&gt; 
<DIV></DIV>&gt; Timer timer = new Timer(); 
<DIV></DIV>&gt; timer.Interval = 500; 
<DIV></DIV>&gt; timer.Elapsed += new ElapsedEventHandler(OnTimeElapsed); 
<DIV></DIV><FONT color=#ff0000><EM>//This sets the SynchronizingObject property to </EM></FONT>
<DIV></DIV><FONT color=#ff0000><EM>//&nbsp;&nbsp; synchronize with the current window or form object </EM></FONT>
<DIV></DIV><FONT color=#ff0000><EM>//Setting the below property should force the Timer to use the same thread as the UI </EM></FONT>
<DIV></DIV><FONT color=#ff0000><EM>//&nbsp;&nbsp; so they don't get out of synch; otherwise, it would use it's own worker thread. </EM></FONT>
<DIV></DIV><FONT color=#ff0000><EM>timer.SynchronizingObject = this; </EM></FONT>
<DIV></DIV>
<DIV></DIV>&gt; timer.Start(); 
<DIV></DIV>
<DIV></DIV>Another thing you might try doing in addition to the above: 
<DIV></DIV>
<DIV></DIV>&gt; private void OnTimeElapsed(object o, ElapsedEventArgs args) 
<DIV></DIV>&gt; { 
<DIV></DIV>
<P><FONT color=#ff0000><EM>&gt; myCounter++; REMOVE THIS LINE</EM></FONT></P>
<P><FONT color=#ff0000><EM>&gt; myCountLabel.Text = Convert.ToString(Convert.ToInt32(myCounter.Text)+1)); </EM></FONT></P>
<P><FONT color=#ff0000><EM>//This would merely be so you are not storing the value in the label object as well as the myCounter variable</EM></FONT></P>
<DIV></DIV>&gt; 
<DIV></DIV>&gt; Console.WriteLine(myCounter.ToString()); 
<DIV></DIV>
<P>&gt; } </P>
<P>I hope this helps.&nbsp;&nbsp;Let me know if this doens't improve anything. </P>
<DIV></DIV>
<DIV></DIV>Thanks! 
<DIV></DIV>
<DIV></DIV>
<P>Antonio </P>
<P>&nbsp;</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>&gt;From: Jimmy Do <CRISPYLEAVES@GMAIL.COM>
<DIV></DIV>&gt;Reply-To: Jimmy Do <CRISPYLEAVES@GMAIL.COM>
<DIV></DIV>&gt;To: mono-list@lists.ximian.com 
<DIV></DIV>&gt;Subject: [Mono-list] Label stops updating during System.Timers.Timer callbacks 
<DIV></DIV>&gt;Date: Tue, 21 Sep 2004 08:53:17 -0700 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Hi everyone, 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;I was just experimenting with System.Timers.Timer in Mono and found 
<DIV></DIV>&gt;something strange happening. First, I created a window with a Label in 
<DIV></DIV>&gt;it. Then I setup a Timer to go off every second. In the 
<DIV></DIV>&gt;ElapsedEventHandler callback, I set the label's text equal to a 
<DIV></DIV>&gt;counter that I increment every time the callback is invoked. The 
<DIV></DIV>&gt;application works for a little bit and I see the label counting up 
<DIV></DIV>&gt;from 1, but the label eventually stops updating. Strangely, some debug 
<DIV></DIV>&gt;output with Console.WriteLine shows that the callback is still being 
<DIV></DIV>&gt;invoked. 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;The label usually stops updating after I *drag the parent window 
<DIV></DIV>&gt;around the screen*. After it stops updating, if you drag a second 
<DIV></DIV>&gt;window on top of the label and then move the window away, the label 
<DIV></DIV>&gt;will not redraw and you'll be left with an empty parent window. 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Some info about my setup: 
<DIV></DIV>&gt;Mono 1.0.1 
<DIV></DIV>&gt;MonoDevelop 0.5 
<DIV></DIV>&gt;Fedora Core 2 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Here's the code: 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;////////////////////// 
<DIV></DIV>&gt;// Main.cs 
<DIV></DIV>&gt;////////////////////// 
<DIV></DIV>&gt;using System; 
<DIV></DIV>&gt;using Gtk; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;class TimerApp 
<DIV></DIV>&gt;{ 
<DIV></DIV>&gt; public static void Main(string[] args) 
<DIV></DIV>&gt; { 
<DIV></DIV>&gt; Application.Init(); 
<DIV></DIV>&gt; new TimerWindow(); 
<DIV></DIV>&gt; Application.Run(); 
<DIV></DIV>&gt; } 
<DIV></DIV>&gt;} 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;////////////////////////////// 
<DIV></DIV>&gt;// TimerWindow.cs 
<DIV></DIV>&gt;////////////////////////////// 
<DIV></DIV>&gt;using System; 
<DIV></DIV>&gt;using System.Timers; 
<DIV></DIV>&gt;using Gtk; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;public class TimerWindow : Window 
<DIV></DIV>&gt;{ 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; public TimerWindow() : base("TimerWindow") 
<DIV></DIV>&gt; { 
<DIV></DIV>&gt; this.DeleteEvent += new DeleteEventHandler(OnWindowDelete); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; VBox vbox = new VBox(); 
<DIV></DIV>&gt; myCountLabel = new Label("-"); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; vbox.PackStart(new Label("The number below will count up:")); 
<DIV></DIV>&gt; vbox.PackStart(myCountLabel); 
<DIV></DIV>&gt; this.Add(vbox); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; myCounter = 0; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; Timer timer = new Timer(); 
<DIV></DIV>&gt; timer.Interval = 500; 
<DIV></DIV>&gt; timer.Elapsed += new ElapsedEventHandler(OnTimeElapsed); 
<DIV></DIV>&gt; timer.Start(); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; this.ShowAll(); 
<DIV></DIV>&gt; } 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; private void OnWindowDelete(object o, DeleteEventArgs args) 
<DIV></DIV>&gt; { 
<DIV></DIV>&gt; Application.Quit(); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; } 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; private void OnTimeElapsed(object o, ElapsedEventArgs args) 
<DIV></DIV>&gt; { 
<DIV></DIV>&gt; myCounter++; 
<DIV></DIV>&gt; myCountLabel.Text = myCounter.ToString(); 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; Console.WriteLine(myCounter.ToString()); 
<DIV></DIV>&gt; } 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; private int myCounter; 
<DIV></DIV>&gt; private Label myCountLabel; 
<DIV></DIV>&gt;} 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Thanks for any help! 
<DIV></DIV>&gt; 
<DIV></DIV>&gt;Jimmy 
<DIV></DIV>&gt;_______________________________________________ 
<DIV></DIV>&gt;Mono-list maillist&nbsp;&nbsp;-&nbsp;&nbsp;Mono-list@lists.ximian.com 
<DIV></DIV>&gt;http://lists.ximian.com/mailman/listinfo/mono-list 
<DIV></DIV></div></html>