您好,我已经解决了一些与计时器相关的问题。 希望有人能帮忙..
- 我有一个包含按钮的窗体
- 当我点击那个按钮时,我启动了一个参数化线程
Thread thread1 = new Thread(new ParameterizedThreadStart( execute2));
thread1.Start(externalFileParams);
- 线程里面的代码执行的很好
- 在这个线程的最后一行我启动了一个计时器
.
public void execute2(Object ob)
{
if (ob is ExternalFileParams)
{
if (boolean_variable== true)
executeMyMethod();//this also executes very well if condition is true
else
{
timer1.enabled = true;
timer1.start();
}
}
}
}
5 但是定时器的滴答事件没有触发
我正在开发 VS2008 3.5 框架。我从工具箱中拖出计时器并将其 Interval
设置为 300 还尝试设置 Enabled
true/false
方法是 timer1_Tick(Object sender , EventArgs e)
但它没有被触发
谁能指出我做错了什么?
最佳答案
您可以尝试以这种方式启动计时器:
在表单构造函数中添加:
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 1 second.
aTimer.Interval = 1000;
将此方法添加到 Form1:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
//do something with the timer
}
在按钮点击事件中添加:
aTimer.Enabled = true;
这个计时器已经线程化了,所以不需要启动一个新的线程。
关于c# - 从 C# 中的不同线程启动计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5727023/