c# - 从 C# 中的不同线程启动计时器

标签 c# multithreading visual-studio-2008 timer

您好,我已经解决了一些与计时器相关的问题。 希望有人能帮忙..

  1. 我有一个包含按钮的窗体
  2. 当我点击那个按钮时,我启动了一个参数化线程
Thread thread1 = new Thread(new ParameterizedThreadStart( execute2));
thread1.Start(externalFileParams);
  1. 线程里面的代码执行的很好
  2. 在这个线程的最后一行我启动了一个计时器

.

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/

相关文章:

c# - 这把锁有什么问题?

c# - 用于 Linux 的 WinForms 设计器

c# - 动态标签 : Adding them using a dataset and checking if they exist

c# - 如何测试 Linq-To-SQL ConnectionString 以确保其有效

ios - 在后台线程上运行方法使动画在一段时间后停止

c# - 每n秒更新一次对象状态

Java ForkJoinPool 线程未完成

jquery - 通过 jquery 使用类时消除 Visual Studio 2008 中的 CSS 警告

wpf - Visual Studio 2008 Xaml 编辑器不工作/消失

visual-studio-2008 - 有什么方法可以中断在Visual Studio中执行的下一行代码吗?