Backgroundworker暂停和恢复。 [英] Backgroundworker pause and resume.

查看:217
本文介绍了Backgroundworker暂停和恢复。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家。



在我的c#windows应用程序中,我有两个按钮(导出和取消)。



在导出按钮中我启动一个后台工作程序 - 它会将数据库表数据导出到.csv文件。



当用户点击取消按钮时,我正在显示消息框(取消操作,是否要继续单击是),是或否选项。



这里我的要求是什么时候消息框出现我想暂停后台工作。



如果用户点击是,我想取消mackgroundworker(这是有效的)。

如果用户点击否,我想恢复背景工作者。



请分享您的解决方案。

Dear Experts.

In my c# windows application, i have two buttons(export and cancel).

In export button i am starting one background worker-it will export database table data to the .csv file.

When ever the user clicks cancel button, I am displaying the message box("cancel the operation, do you want to proceed click yes") with yes or no options.

Here my requirement is when the when the message box appears i want to Pause the backgroundworker.

If user clicks yes i want to cancel the mackgroundworker(this is working).
If user clicks no i want to resume the backgrouondworker.

Kindly share your solutions.

推荐答案

假设你的线程方法有一个循环,你正在编写从DB到File的数据传输,你必须在那里添加一个condtion来保持你的线程。

这里是一个例子!







lets say your thread method have a loop in wicth you are coding the data transfer from DB to File, you have to add a condtion in there to put your thread on hold.
here is an exemple!



//class level declaration!
public AutoResetEvent autoRest = new AutoResetEvent(false);
bool stopThread = false;


your thread method
Loop
{
//transfer data from Db to .CSV 
//one line at a time or block at a time

if(stopThread == true)
   autoRest.WaitOne(); 

}





所以如果你把你的stopThread设置为true,你的线程会将状态从runing改为waitSleepJoin 。如果你打电话给另一个帖子,它会继续工作:





so if you put your stopThread to true, your thread will change status from runing to waitSleepJoin. it will continue its work if you call in another thread:

autoRest.Set();





还有很多其他方法可以保持你的线程,

希望它有所帮助。



there are many other ways to put your thread on hold,
Hope it helps.


不是后台工作者,线程,任何线程。



嗯,暂停恢复已弃用不安全的。基本上,线程可以获取线程同步对象并在释放之前暂停。它可以很容易地阻塞整个进程或它的某些线程,直到进程被某些外部进程终止 - 如果某些非后台线程正在处理,被干扰的进程可能无法自行终止。



安全实现类似的行为可以使用线程同步原语 EventWaitHandle 和从它派生的类来完成。请参阅:

http://msdn.microsoft.com /en-us/library/system.threading.eventwaithandle.aspx [ ^ ]。



在某些点上,线程应该调用方法 WaitOne ()此类的某个实例。如果实例处于等待状态(重置),则调用线程将切换到我的OP并进入等待(休眠)状态,在此状态下不占用任何CPU时间。在被唤醒之前,它不会被安排回执行。它可以被几个事件唤醒,例如 Thread.Abort ,超时(如果有的话),或者重要的是,当某个其他线程发出相同的<$ c实例时$ c> EventWaitHandle ,将其置于信号状态(设置)。因此,控制 EventWaitHandle 的实例,某些线程可以限制某些其他线程的执行,有效地实现类似暂停的操作/ 恢复,但是在线程运行的代码的某个固定点上。



即使线程是在获得其他一些事件同步原语对象的所有权之后暂停,如果总是可以中止(见上文),即使持有该线程的 EventWaitHandle 的实例是保持无信号(重置)。



现在,关于取消线程。 Thread.Abort 是线程终止的异步方法,许多人认为这种方法不安全(但这是一个尚未弃用的重要方法)。事实上,如果你需要使用它,你需要非常清楚你在做什么。基于异常播种机制,其机制相对较新,极其聪明。也许这不是在这里讨论它的地方。请查看我过去的答案:

正确关闭dll中的线程 [ ^ ],

从专用线程创建进程的问题 [ ^ ]。



合作社线程取消,请参阅此微软文章:

http:// msdn。 microsoft.com/en-us/library/dd997364.aspx [ ^ ]。



-SA
Not background worker, a thread, any thread.

Well, Pause and Resume were deprecated as unsafe. Basically, a thread can get a thread synchronization object and get paused before releasing it. It can easily jam the whole process or some threads of it until the process is terminated by some external process — the jammed process might not be able to terminate itself if some non-background threads are handing.

The safe implementation is similar behavior can be done using thread synchronization primitives EventWaitHandle and classes derived from it. Please see:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^].

In certain points, the thread should call the method WaitOne() of some instance of this class. If the instance is in the wait state (reset), the calling thread will be switched of my OP and put in a wait (sleeping) state where it does not spend any CPU time. It won't be scheduled back to execution until it is waken up. It can be waken up by several events, such as Thread.Abort, timeout (if any), or, importantly, when some other thread signals the same instance of EventWaitHandle, put it to signaled state (set). So, controlling the instance of EventWaitHandle, some thread can throttle execution of some other thread, effectively implementing something similar to Pause/Resume, but in some fixed point of the code running by the thread.

Even if the thread is "paused" this way after it got ownership of some other event synchronization primitive object, if can always be aborted (see above), even if the instance of EventWaitHandle holding the thread is kept non-signaled (reset).

Now, about cancellation of the thread. Thread.Abort is the asynchronous method of thread termination, which many consider as unsafe (but this is important method which has not been deprecated). Indeed, you need to know perfectly well what are you doing, if you need to use it. Its mechanism is relatively new and extremely clever, based on the mechanism of exception seeding. Maybe this is not a place to discuss it here. Please see my past answers:
Close correcly the thread inside a dll[^],
Problem with creating Process from dedicated thread[^].

For cooperative thread cancellation, please see this Microsoft article:
http://msdn.microsoft.com/en-us/library/dd997364.aspx[^].

—SA


这篇关于Backgroundworker暂停和恢复。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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