我正在使用Timer Tick事件每秒钟执行一次方法。我看到这些方法消耗的时间以毫秒为单位,因此我尝试了以下代码,但仍未在每个tick事件中精确地执行一秒钟:
private void timer1_Tick(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Method1();
Method2();
Method3();
sw.Stop();
TimeSpan ts = sw.Elapsed;
string elapsedTime = String.Format("{0:00}", ts.Milliseconds);
timer1.Interval = 1000 - ts.Milliseconds;
label1.Text = timer1.Interval.ToString();
label2.Text = elapsedTime;
}