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

发布于 2024-11-02 07:18:38 字数 877 浏览 1 评论 0原文

您好,我遇到了一些与计时器相关的问题。 希望有人可以提供帮助..

  1. 我有一个包含按钮的Windows窗体
  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) 但它没有被触发

有人可以建议我做错了什么吗?

Hi i have stepped into some problem related to timer.
hope somebody can help..

  1. I have a windows form containing a button
  2. when i click on that button i start a parameterised thread
Thread thread1 = new Thread(new ParameterizedThreadStart( execute2));
thread1.Start(externalFileParams);
  1. the code inside the thread executes very well
  2. at the last line of this thread i start a timer

.

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 but the tick event of the timer is not fired

I am working on VS2008 3.5 framework. I have dragged the timer from toolbox and set its Interval to 300 also tried to set Enabled true/false
method is timer1_Tick(Object sender , EventArgs e) but its not fired

can anybody suggest what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

草莓味的萝莉 2024-11-09 07:18:39

您可以尝试以这种方式启动计时器:

在表单构造函数中添加此方法:

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;

此计时器已经是线程化的,因此无需启动新线程。

You could try to start the timer this way:

Add in form constructor this:

System.Timers.Timer aTimer = new System.Timers.Timer();
 aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
 // Set the Interval to 1 second.
 aTimer.Interval = 1000;

Add this method to Form1:

 private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
   //do something with the timer
 }

On button click event add this:

aTimer.Enabled = true;

This timer is already threaded so no need to start a new thread.

写给空气的情书 2024-11-09 07:18:39

Matías Fidemraizer 所说的确实如此。但是,有一种解决方法......

当您的表单上有一个可调用的控件(例如状态栏)时,只需调用该控件即可!

C# 代码示例:

private void Form1_Load(object sender, EventArgs e)
{
    Thread sampleThread = new Thread(delegate()
    {
        // Invoke your control like this
        this.statusStrip1.Invoke(new MethodInvoker(delegate()
        {
            timer1.Start();
        }));
    });
    sampleThread.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    MessageBox.Show("I just ticked!");
}

It is true what Matías Fidemraizer says. But, there is a work around...

When you have a control on your form that is invokable (eg. a statusbar), just invoke that one!

C# Code sample:

private void Form1_Load(object sender, EventArgs e)
{
    Thread sampleThread = new Thread(delegate()
    {
        // Invoke your control like this
        this.statusStrip1.Invoke(new MethodInvoker(delegate()
        {
            timer1.Start();
        }));
    });
    sampleThread.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    MessageBox.Show("I just ticked!");
}
沧桑㈠ 2024-11-09 07:18:39

System.Windows.Forms.Timer 在单线程应用程序中工作。

检查此链接:

备注说:

计时器用于在以下时间引发事件
用户定义的间隔。这个Windows
定时器设计用于
UI 的单线程环境
线程用于执行
加工。它要求用户
代码有一个可用的 UI 消息泵
并且始终从相同的位置进行操作
线程,或将调用编组到
另一个线程。

阅读更多“备注”部分,您会发现微软建议您使用此计时器将其与UI线程同步。

System.Windows.Forms.Timer works in a single-threaded application.

Check this link:

Remarks says:

A Timer is used to raise an event at
user-defined intervals. This Windows
timer is designed for a
single-threaded environment where UI
threads are used to perform
processing. It requires that the user
code have a UI message pump available
and always operate from the same
thread, or marshal the call onto
another thread.

Read more "Remarks" section and you'll find that Microsoft recommends that you use this timer synchronizing it with the UI thread.

永不分离 2024-11-09 07:18:39

我会使用 BackgroundWorker (而不是 原始线程)。主线程将订阅工作线程的 RunWorkerCompleted 事件:当线程完成时,该事件在主线程中触发。使用此事件处理程序重新启动计时器。

I would use a BackgroundWorker (instead of a raw thread). The main thread would subscribe to the worker's RunWorkerCompleted event: The event fires in your main thread when the thread completes. Use this event handler to restart your timer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文