秒表或计时器
StopWatch Or Timer
我认为计时器在显示 _Tick()
方法的时间时不是很准确。我想在 minutes/seconds 中显示已用时间 显示完成一个过程所需的时间长度。我已经将它与 Timer 一起使用,但发现它的计算不正确。这就是为什么我想问 StopWatch 会更好以更准确地显示还是它们是我应该一起使用的单独控件?
private int timepassed = 0;
private void buttonFourteen_Click(object sender, DragEventArgs e)
{
timer2.Start();
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (s, e) =>
{
//Lengthy Procedure Goes Here
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
timer2.Stop();
};
backgroundWorker.RunWorkerAsync();
}
private void timer2_Tick(object sender, EventArgs e)
{
timepassed++;
timedisplay.Text = timepassed.ToString();
}
System.Diagnostics.StopWatch
是跟踪这类事情的典型方法。如果您正在使用 VS 并且只是尝试进行性能检查,您还可以分析代码,同时让您清楚地了解每种方法花费了多少时间。
这是用秒表完成此操作的一种方法,它应该非常准确:
private readonly Stopwatch sw = new Stopwatch();
private readonly System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
timer.Tick += timer2_Tick;
}
private void buttonFourteen_Click(object sender, EventArgs e)
{
sw.Restart();
timer.Start();
var backgroundWorker = new BackgroundWorker();
// Simulating a 10-second process for testing
backgroundWorker.DoWork += (s, ea) => Thread.Sleep(TimeSpan.FromSeconds(10));
backgroundWorker.RunWorkerCompleted += (s, ea) => timer.Stop();
backgroundWorker.RunWorkerAsync();
}
private void timer2_Tick(object sender, EventArgs e)
{
timedisplay.Text = sw.Elapsed.ToString("g");
// Or use a custom time format (you can specify precision as well as
// delimiters between days, hours, minutes, seconds, and milliseconds):
// timedisplay.Text = sw.Elapsed.ToString(@"dd\.hh\:mm\:ss\.ff");
}
我认为计时器在显示 _Tick()
方法的时间时不是很准确。我想在 minutes/seconds 中显示已用时间 显示完成一个过程所需的时间长度。我已经将它与 Timer 一起使用,但发现它的计算不正确。这就是为什么我想问 StopWatch 会更好以更准确地显示还是它们是我应该一起使用的单独控件?
private int timepassed = 0;
private void buttonFourteen_Click(object sender, DragEventArgs e)
{
timer2.Start();
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (s, e) =>
{
//Lengthy Procedure Goes Here
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
timer2.Stop();
};
backgroundWorker.RunWorkerAsync();
}
private void timer2_Tick(object sender, EventArgs e)
{
timepassed++;
timedisplay.Text = timepassed.ToString();
}
System.Diagnostics.StopWatch
是跟踪这类事情的典型方法。如果您正在使用 VS 并且只是尝试进行性能检查,您还可以分析代码,同时让您清楚地了解每种方法花费了多少时间。
这是用秒表完成此操作的一种方法,它应该非常准确:
private readonly Stopwatch sw = new Stopwatch();
private readonly System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
timer.Tick += timer2_Tick;
}
private void buttonFourteen_Click(object sender, EventArgs e)
{
sw.Restart();
timer.Start();
var backgroundWorker = new BackgroundWorker();
// Simulating a 10-second process for testing
backgroundWorker.DoWork += (s, ea) => Thread.Sleep(TimeSpan.FromSeconds(10));
backgroundWorker.RunWorkerCompleted += (s, ea) => timer.Stop();
backgroundWorker.RunWorkerAsync();
}
private void timer2_Tick(object sender, EventArgs e)
{
timedisplay.Text = sw.Elapsed.ToString("g");
// Or use a custom time format (you can specify precision as well as
// delimiters between days, hours, minutes, seconds, and milliseconds):
// timedisplay.Text = sw.Elapsed.ToString(@"dd\.hh\:mm\:ss\.ff");
}