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

Jimmy Do Jimmy Do <crispyleaves@gmail.com>
Tue, 21 Sep 2004 08:53:17 -0700


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