[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. Very true. 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. I guess you can't do that with a Gtk window. 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>>From: Jimmy Do <crispyleaves@gmail.com>
<DIV></DIV>>Reply-To: Jimmy Do <crispyleaves@gmail.com>
<DIV></DIV>>To: Antonio Santana <antjensavwork@hotmail.com>
<DIV></DIV>>Subject: Re: [Mono-list] Label stops updating during System.Timers.Timer callbacks
<DIV></DIV>>Date: Tue, 21 Sep 2004 16:20:45 -0700
<DIV></DIV>>
<DIV></DIV>>Hmm....
<DIV></DIV>>
<DIV></DIV>>timer.SynchronizingObject = this;
<DIV></DIV>>gives a compile-time error:
<DIV></DIV>>"Cannot convert implicitly from `TimerWindow' to
<DIV></DIV>>`System.ComponentModel.ISynchronizeInvoke'(CS0029)"
<DIV></DIV>>
<DIV></DIV>>'this' refers to a Gtk.Window, so maybe that's why it doesn't work?
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>Jimmy
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>----- Original Message -----
<DIV></DIV>>From: Antonio Santana <antjensavwork@hotmail.com>
<DIV></DIV>>Date: Tue, 21 Sep 2004 14:19:10 -0500
<DIV></DIV>>Subject: RE: [Mono-list] Label stops updating during
<DIV></DIV>>System.Timers.Timer callbacks
<DIV></DIV>>To: crispyleaves@gmail.com, mono-list@lists.ximian.com
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>You might try the following:
<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>>//This sets the SynchronizingObject property to
<DIV></DIV>>// synchronize with the current window or form object
<DIV></DIV>>//Setting the below property should force the Timer to use the same
<DIV></DIV>>thread as the UI
<DIV></DIV>>// so they don't get out of synch; otherwise, it would use it's own
<DIV></DIV>>worker thread.
<DIV></DIV>>timer.SynchronizingObject = this;
<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>>
<DIV></DIV>>
<DIV></DIV>> > myCounter++; REMOVE THIS LINE
<DIV></DIV>>
<DIV></DIV>> > myCountLabel.Text = Convert.ToString(Convert.ToInt32(myCounter.Text)+1));
<DIV></DIV>>
<DIV></DIV>>//This would merely be so you are not storing the value in the label
<DIV></DIV>>object as well as the myCounter variable
<DIV></DIV>> >
<DIV></DIV>> > Console.WriteLine(myCounter.ToString());
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>> > }
<DIV></DIV>>
<DIV></DIV>>I hope this helps. Let me know if this doens't improve anything.
<DIV></DIV>>
<DIV></DIV>>Thanks!
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>
<DIV></DIV>>Antonio
<DIV></DIV></div></html>