Timer

from https://www.mono-project.com/docs/gui/gtksharp/responsive-applications/


Timeouts

You can use timeouts to invoke routines at specified intervals of time. This can be used to update status at specified times for example while a background thread runs or to notify the user of an action at a given interval.
Those of you who have done winforms development before will notice that GLib.Timeout is similar to System.Windows.Forms.Timer. The difference is that GLib.Timeouts are always invoked on the thread that owns the Gtk mainloop.
     void StartClock ()
     {
         // Every second call `update_status' (1000 milliseconds)
 
         GLib.Timeout.Add (1000, new GLib.TimeoutHandler (update_status));
     }
 
     bool update_status ()
     {
         time_label.Text = DateTime.Now.ToString ();
 
         // returning true means that the timeout routine should be invoked
         // again after the timeout period expires.   Returning false would
         // terminate the timeout.
 
         return true;
     }





from http://simplesandsamples.com/gui-timer.cs.html


(Forms)タイマーを使う

    gui-timer.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Drawing;
using System.Windows.Forms;
class Test : Form
{
Timer t;
Test()
{
this.Size = new Size(300,200);
t = new Timer();
t.Enabled = true;
t.Interval = 5000; // 5 sec
t.Tick += new EventHandler(OnTimer);
}
void OnTimer(object sender, EventArgs e)
{
Application.Exit();
}
[STAThread]
public static void Main()
{
Application.Run(new Test());
}
}
$ mcs gui-timer.cs -r:System.Windows.Forms -r:System.Drawing
$ mono gui-timer.exe 

留言

熱門文章