我在给定的特定时间运行backgroundworker时遇到问题.
private void button1_Click(object sender,EventArgs e) { backgroundWorker1.RunWorkerAsync(); }
然后在backgroundWorker1_DoWork中:
private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e) { backgroundWorker1.CancelAsync(); }
最后在backgroundWorker1_RunWorkCompleted中:
private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e) { backgroundWorker1.RunWorkerAsync(); }
我想连续运行背景,但每5秒或超过5秒.
如果有人能帮助我,我将非常感激,
提前致谢.
解决方法
运行
Timer类表示Timer控件,用于在运行时创建Timer.以下代码段在运行时创建Timer,设置其属性和事件处理程序.
Timer t = new Timer(); t.Interval = 2000; timer1.Enabled = true; timer1.Tick += new System.EventHandler(OnTimerEvent);
事件处理程序代码如下所示.
private void OnTimerEvent(object sender,EventArgs e) { backgroundWorker1.RunWorkerAsync(); }
这是演示:C# Timer Tutorial
检查msdn:http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx上的文档