[Mono-list] Label stops updating during System.Timers.Timer callbacks
Jimmy Do
Jimmy Do <crispyleaves@gmail.com>
Tue, 21 Sep 2004 16:28:40 -0700
Ah, I see. I hadn't considered that. Some have suggested using
Gtk.Timeout instead of System.Timers.Timer so I'll look into that
instead.
Thanks!
Just curious though...in what situation can we use a
System.Timers.Timer with a GUI widget? Only with WinForms?
Jimmy
On Tue, 21 Sep 2004 19:25:13 +0200, Chris Turchin <chris@turchin.net> wrote:
> I think this is because the gui in gtk apps must be updated by the main
> thread and the timer call is probably happening elsewhere, but you'd
> best wait for a more authorative response from someone who really knows
> gtk...
>
> from the FAQ (http://gtk-sharp.sourceforge.net/faq.html):
>
> 3.5 Why does my multithreaded app hang?
> Gtk# is not threadsafe. Neither is System.Windows.Forms. Gtk+ is "thread
> aware" and provides a mechanism for locking and unlocking the UI
> updates, but you are still responsible for ensuring that all updates to
> the GUI come from the main thread where you called Run (). Gtk# provides
> a helper class called ThreadNotify to assist in the context switch from
> different threads to the GUI thread. In addition, using an Idle handler
> or a GLib.TimeoutHandler with a very short interval will ensure the
> context switch.
>
>
>
>
> On Tue, 2004-09-21 at 08:53 -0700, Jimmy Do wrote:
> > Hi everyone,
> >
> > I was just experimenting with System.Timers.Timer in Mono and found
> > something strange happening. First, I created a window with a Label in
> > it. Then I setup a Timer to go off every second. In the
> > ElapsedEventHandler callback, I set the label's text equal to a
> > counter that I increment every time the callback is invoked. The
> > application works for a little bit and I see the label counting up
> > from 1, but the label eventually stops updating. Strangely, some debug
> > output with Console.WriteLine shows that the callback is still being
> > invoked.
> >
> > The label usually stops updating after I *drag the parent window
> > around the screen*. After it stops updating, if you drag a second
> > window on top of the label and then move the window away, the label
> > will not redraw and you'll be left with an empty parent window.
> >
> > Some info about my setup:
> > Mono 1.0.1
> > MonoDevelop 0.5
> > Fedora Core 2
> >
> >
> > Here's the code:
> >
> > //////////////////////
> > // Main.cs
> > //////////////////////
> > using System;
> > using Gtk;
> >
> > class TimerApp
> > {
> > public static void Main(string[] args)
> > {
> > Application.Init();
> > new TimerWindow();
> > Application.Run();
> > }
> > }
> >
> >
> > //////////////////////////////
> > // TimerWindow.cs
> > //////////////////////////////
> > using System;
> > using System.Timers;
> > using Gtk;
> >
> > public class TimerWindow : Window
> > {
> >
> > public TimerWindow() : base("TimerWindow")
> > {
> > this.DeleteEvent += new DeleteEventHandler(OnWindowDelete);
> >
> > VBox vbox = new VBox();
> > myCountLabel = new Label("-");
> >
> > vbox.PackStart(new Label("The number below will count up:"));
> > vbox.PackStart(myCountLabel);
> > this.Add(vbox);
> >
> >
> > myCounter = 0;
> >
> > Timer timer = new Timer();
> > timer.Interval = 500;
> > timer.Elapsed += new ElapsedEventHandler(OnTimeElapsed);
> > timer.Start();
> >
> >
> > this.ShowAll();
> > }
> >
> > private void OnWindowDelete(object o, DeleteEventArgs args)
> > {
> > Application.Quit();
> >
> > }
> >
> > private void OnTimeElapsed(object o, ElapsedEventArgs args)
> > {
> > myCounter++;
> > myCountLabel.Text = myCounter.ToString();
> >
> > Console.WriteLine(myCounter.ToString());
> > }
> >
> > private int myCounter;
> > private Label myCountLabel;
> > }
> >
> >
> >
> > Thanks for any help!
> >
> > Jimmy
> > _______________________________________________
> > Mono-list maillist - Mono-list@lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-list
> --
> Chris Turchin <chris@turchin.net>
>
>