【发布时间】:2020-12-22 09:33:24
【问题描述】:
我的窗体应用程序的任务是在时间等于 10 秒时调用方法和 checkBox1_CheckedChanged 事件。但是程序不能去 if 语句做某事。
计时器:
private void timerAU_Tick(object sender, EventArgs e)
{
stopwatch.Start();
time = stopwatch.Elapsed.ToString(@"hh\:mm\:ss\.f");
TimerAutoUpdateHelper();
if (time.Equals("00:00:10.1"))
{
stopwatch.Reset();
timerAU.Stop();
thread.Join();
thread.Abort();
MessageBox.Show("OK!");
checkBox1_CheckedChanged(sender, e);
}
}
复选框:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
timerAU.Start();
stopwatch.Start();
thread = new Thread(() => DataProcess.DataFromPLC(new Form1()));
thread.Start();
Log.WriteLogFile(EventCategory.AUTOUPDATECHECKED);
}
else
{
stopwatch.Stop();
MessageBox.Show(time);
checkBox1.Checked = false;
timerAU.Stop();
thread.Join();
thread.Abort();
Log.WriteLogFile(EventCategory.AUTOUPDATEUNCHECKED);
}
}
【问题讨论】:
-
thread.Abort();oh mamma mia, mamma mia .... 永远不要使用它。为了帮助您解决问题,需要更多上下文。那是什么样的计时器? TimerAutoUpdateHelper 做什么? ... -
秒表也是定时器控件吗?
-
TimerAutoUpdateHelper 方法包含这个从 PLC 查询数据的调用方法。
-
哪个If语句不能通过?在
timerAU_Tick或checkBox1_CheckedChanged内? -
嗯,问题是:你做这件事的方式,它永远不会是你所期望的价值。另一件事是:只有当它是确切值时,条件才会为真。不是 10.2 不是 15.7 ,正好是 10.1。所以你可能想要
if( stopwatch.Elapsed > TimeSpan.FromSeconds(10) )
标签: c# visual-studio winforms visual-studio-2017