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