System.Windows.Forms.Timer event doesn't fire at all when run under mono
I compiled my application with monodevelop. And tried to run with mono on Linux. It seems that the System.Windows.Form开发者_开发问答s.Timer objects' tick event never fires.
It runs without problems on windows, but not in Linux.
The related code roughly looks like this:
// derived from a From
//...
private Timer controlTimer;
//....
protected override OnCreateControl(/*...*/)
{
//,,,
controlTimer=new Timer();
controlTimer.Tick += new EventHandler(controlFunc);
controlTimer.Interval = 40;
controlTimer.Tag = this; // The form is used by the callback
controlTimer.Start();
//...
}
//....
Even the simpliest winform application does not run the timer under mono...
But it runs if I place the initialization to the form1_load and then add it to the form.load event.
EDIT: OnCreateControl is working if you put a base.OnCreateControl() at the end of the method. On .NET it works without that line and worked under mono too before...
So it seems it's a bug in mono and I will report it.
Try using System.Timers instead of Windows.Forms.Timer
see: Timer won't tick
also plenty of things from Windows.Forms seem not to be implemented in Mono at all (for example Invoke)
That fixed it for me.
Test it with mono 2.6 preview or latest svn. If it still doesn't work than submit a bug report.
I would suggest adding a Console.WriteLine in your OnControlCreated method to ensure that the method is getting called and the Timer is indeed being created.
精彩评论