简体   繁体   中英

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.Forms.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.

我建议您在OnControlCreated方法中添加Console.WriteLine以确保调用该方法并且确实创建了Timer。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM