后台工作者和计时器 [英] Background Worker and Timer

查看:55
本文介绍了后台工作者和计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写此应用程序.目前,我有一个BackgroundWorker线程来执行一些操作.在worker线程中,我有一个循环来完成 n 次工作,还有一个我用来暂停循环的延迟时间.

目前看起来像这样

Hi,

I am trying to write this application. Currently I have a BackgroundWorker thread to do some operation. Inside the worker thread, I have this loop to do the work n amount of times and also a delay time in which I used to pause the loop.

Currently it looks something like this

 bg_DoWork(object sender, DoWorkEventArgs e)
{ ......
  repeat = 10;
  int i = 0;
  int delay = 5;

  while ( i < repeat)
  {  //do some work 
      i++;
      //pause for n seconds
      System.Threading.Thread.Sleep(delay*1000)
  }



这很好.但是,我希望在UI线程中有一个计时器可以在延迟时间内递增或递减计数.我尝试在后台线程中创建计时器,但由于它不在UI线程中,因此不会调用tick事件.最好的方法是什么?

谢谢
dbzx



This works fine. However, I want have a timer to count down or up during the delay duration, in the UI thread. I tried creating a timer in the background thread but the tick event won''t invoke because it is not in the UI thread. What is the best way of doing this?

Thank you,
dbzx

推荐答案

我仍然不确定您确实需要该计时器,因为您并未真正解释其目的,但是如果这样做,最好的方法是不使用System.Windows.Forms.Timer. .NET中至少还有三个计时器,而这是最差的.仅在对精度没有要求并且可以忍受100%或更高的时序误差的情况下,才可以使用它.此计时器的唯一一个好问题是,但值得怀疑的功能是简化了它的使用:它的tick事件处理程序在UI线程中也被调用,在您的情况下,这也是一个麻烦. (但是,您没有标记UI库,因此我不确定您是否尝试使用System.Windows.Forms,但是我认为这很有可能.)

您可以选择System.Threading.TimerSystem.Timers.TimerSystem.Windows.Threading.DispatcherTimer(WPF).最有可能的是,您宁愿选择System.Timers.Timer:
http://msdn.microsoft.com/en-us/library/system.timers. timer.aspx [ ^ ].

唯一的困难是:不能保证在UI线程中调用此计时器滴答事件处理程序,因此您将需要使用与在非UI线程中使用的相同的调用机制来委派要在其中执行的某些UI操作. UI线程.您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
I''m still not sure that you really need that timer, as you did not really explain the purpose, but if you do, the best way is not using System.Windows.Forms.Timer. There are at least three more timers in .NET, and this one is the worst. You can use it only if you have no requirements to accuracy and can tolerate the 100% or more of timing error. The only one good but questionable feature of this timer is that its use is simplified: its tick event handler is also called in UI thread, which is, in your case, also a hassle. (However, you did not tag your UI library, so I''m not sure you tried to work with System.Windows.Forms, but I think this is pretty likely.)

You can choose from System.Threading.Timer, System.Timers.Timer or System.Windows.Threading.DispatcherTimer (WPF). Most likely, you would rather prefer System.Timers.Timer:
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx[^].

The only difficulty is: this timer tick event handler is not guaranteed to be called in the UI thread, so you will need to use the same very invocation mechanism you would use in a non-UI thread to delegate some UI operation to be executed in the UI thread. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


我认为:
bg_DoWork(object sender, DoWorkEventArgs e)
{ ......
  repeat = 10;
  int i = 0;
  int delay = 5;
  DateTime d=DateTime.Now;
 
  while ( i < repeat)
  { 
      TimeSpan ts=d.Subtract(DateTime.Now); 
      if(ts.Seconds==5)
        {
         //do some work 
         i++;
         d=DateTime.Now;
        }
      else
      //count down/up any other variable
      
  }


这篇关于后台工作者和计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆