two or more timer with different intervals with C#
I have to a d.b entry in which the intervals are given,
Suppose 1 timer tm_5 will check the entries after every 5 mins开发者_StackOverflow & the timer tm_10 will check
the entries after every 10 mins,
The problem is that it checks only the entries for tm_5 not for the tm_10.
I am using C#.net 2005 & MS sql server 2005.
Try this (add label1 and label2 to form named TimerTester).
public partial class TimerTester : Form
{
public TimerTester()
{
InitializeComponent();
}
Timer _t1;
Timer _t2;
int _it1 = 0, _it2 = 0;
private void Form1_Load(object sender, EventArgs e)
{
_t1 = new Timer() { Interval = 1000 };
_t1.Tick += new EventHandler(_t1_Tick);
_t2 = new Timer() { Interval = 2000 };
_t2.Tick += new EventHandler(_t2_Tick);
_t1.Start(); _t2.Start();
}
void _t1_Tick(object sender, EventArgs e)
{
label1.Text = "t1: " + (_it1++).ToString();
}
void _t2_Tick(object sender, EventArgs e)
{
label2.Text = "t2: " + (_it2++).ToString();
}
}
Also note that you should use Forms.Timer and not Threads.Timer (or use InvokeRequired and read & understand what it implicates).
You should change the text of the label in the UI thread so this code will work. See Control.Invoke and Control.InvokeRequired for help.
精彩评论