如何停止和重新启动BackGroundWorker [英] How to Stop and Restart a BackGroundWorker

查看:284
本文介绍了如何停止和重新启动BackGroundWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在开发一个项目,在两个表单之间使用BackGroundWorker控件。


我需要取消并立即从同一事件重启BackGroundorker。问题在于:当BackGroundWorker.CancelAsync()时,显然线程没有取消,当它到达BackGroundWorker.RunWorkerAsync()时,异常BackgroundWorker忙。抛出。


那么,如何取消并重启同一事件中的后台工作人员。


(注意:我试过线程类也有类似的问题。当使用Thread.Abort()或Thread.Suspended()时,线程停止但它不能再次重启。)

Hi. I''m working on a project in which use a BackGroundWorker control between two forms.

I need to Cancel and immediately to restart the BackGroundorker from the same event. The problem is that: when BackGroundWorker.CancelAsync(), apparently the thread does not cancel and when it gets to BackGroundWorker.RunWorkerAsync(), the exception "BackgroundWorker is busy" is thrown.

So, how can I cancel and restart the backgroundworker from the same event.

(Note: I tried with the Threading Class too and I got a similar problem. When use Thread.Abort() or Thread.Suspended(), the thread is stopped but it cannot restart again.)

推荐答案

我只是因为你的帖子比你大3天而尝试回复。

定义公共变量。说:Public StopRun As Boolean。

在启动BackGroundorker之前将此变量设置为False。

从BackGroundorker不时验证此变量:

如果StopRun然后退出


当你想要停止BackGroundorker时,从主代码设置为True这个变量。


抱歉,我的英语不太好。希望你能得到这个想法。
I try a response only because your post is 3 days older.
Define a public variable. Say: Public StopRun As Boolean.
Before starting the BackGroundorker set this variable to False.
From the BackGroundorker verify from time to time this variable:
If StopRun Then Exit

Set to True this variable from the main code when you wish to stop the BackGroundorker.

Sorry, my English is not very good. Hope you get the idea.


感谢Mihail,但它不起作用。我使用了一个布尔值''StopRun'',当它从一个按钮或另一个独立的方法应用时,但是记住我需要从同一个方法停止并重新启动线程。

基本上,我做了StopRun,然后睡了一些milisecs然后StopRun为False并再次调用线程。但被调用的方法不是从开始就开始,或者只是直到完成才停止。


我还在寻找。
Thanks Mihail but it does not work propertly. I used a boolean ''StopRun'' and when it is applied from a button or from another independent method works, but remember I need to stop and restart the thread from the same method.
Basically, I did StopRun on True, then sleep some milisecs and then StopRun on False and call the thread again. But the called method does not start from the begining or it just does not stop until finish.

I''m still looking for.


你要做什么?

为什么你需要停止BackGroundorker mili 秒。

你知道DoEvents声明吗? />

如果您解释为什么要暂停BackGroundorker,也许我们可以找到解决方案。
What you are trying to do ?
Why you need to stop the BackGroundorker for miliseconds.
Do you know the DoEvents statement ?

If you explain WHY you wish to pause that BackGroundorker, maybe we can find a solution.


这篇关于如何停止和重新启动BackGroundWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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