c# – 后台工作程序中的暂停/恢复循环
发布时间:2020-12-15 04:07:23 所属栏目:百科 来源:网络整理
导读:我在Winform应用程序中的后台工作器中有一个循环. 我刚刚使用了这个Code,但暂停后它不会恢复. 在主类我使用这个 System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(false); 然后在我的开始点击我写了这个: if (!background
我在Winform应用程序中的后台工作器中有一个循环.
我刚刚使用了这个Code,但暂停后它不会恢复. 在主类我使用这个 System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(false); 然后在我的开始点击我写了这个: if (!backgroundWorker1.IsBusy) { MessageBox.Show("Not Busy"); //Just For Debugg _busy.Set(); Start_Back.Text = "Pause"; backgroundWorker1.RunWorkerAsync(tempCicle); } else { _busy.Reset(); Start_Back.Text = "Resume"; } btnStop.Enabled = true; 然后在backgroundworker doWork中我写了这个: private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e) { m_addTab addTabsInvoke = addTabUrl2; Invoke(addTabsInvoke,"http://www.google.com"); foreach (something) { _busy.WaitOne(); if (backgroundWorker1.CancellationPending) { return; } if (tabs.InvokeRequired) { ...... ...... 我不明白为什么暂停工作,而简历不起作用.我错了什么? 解决方法
我猜你想要的最好的猜测:
void ResumeWorker() { // Start the worker if it isn't running if (!backgroundWorker1.IsBusy) backgroundWorker1.RunWorkerAsync(tempCicle); // Unblock the worker _busy.Set(); } void PauseWorker() { // Block the worker _busy.Reset(); } void CancelWorker() { if (backgroundWorker1.IsBusy) { // Set CancellationPending property to true backgroundWorker1.CancelAsync(); // Unblock worker so it can see that _busy.Set(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |