我有两个按钮启动和停止。单击开始按钮时,我想点击表格并循环记录,显示记录在循环中时的时间差(所用时间)。
但是,当单击开始按钮时,由于记录数很高,我无法单击“停止”按钮,我试图单击开始按钮,独立运行该过程,例如,如果处理了 5000 条记录,当我单击停止按钮,我想显示这 5000 条记录所花费的时间,再次单击开始时,继续记录它离开的地方。
我如何独立处理两个事件?任何想法都会非常有帮助。在此先感谢。
private void startQueries()
{
thread=
new Thread(this.LoadResults);
thread.IsBackground =true;
thread.Start();
}
private void LoadResults()
{
//Method that runs a select query - fetches 1000s of records
Timespan startTime = <<Record the start time>>;
//Loop thro' all the 1000s of records;
Timespan stopTime=<<Record the stop time>>;
//Display the difference between start and stop time.
}
private void btnStart_Click(object sender, EventArgs e)
{
if(thread!=null)
{
if (thread.ThreadState == ThreadState.Running)
{
//do nothing
}
else if (thread.ThreadState == ThreadState.Suspended)
{
startQueries();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
//Stop the running thread
// Need to display the time between the thread start and stop.
}
}