开发者

Request thread to quit without forcing it?

I am writing an app. There are plugins which DL resource from the net. If the user would like to quit i would not like any DLs to start but i would like DLs in progress to continue until done. After they stop i would gracefully quit.

How do i find out if a thread is in a middle of a DL or just sleeping? The actual download resource function is in my base class and is not virtual so i can write code that says if its in progress but thats besides the point. How do i actually pass info to the main thread to say if its sleepin开发者_Go百科g and will terminate VS in progress of a download?


The best method is to create an interface that the plugins implement that allow you to query them about their state and request that they stop what they are doing (in case you decide to add that functionality later). Thread.Interrupt and Thread.Abort are highly not recommended. ThreadState is absolutely useless. They're not guaranteed to work and can even leave your code in a lurch, off in unmanaged neverland. By creating an interface that standardizes these requests you can ensure your plugin developers provide the mechanism to report status and safely abort progress.

If you know the developers of the plugins, I suggest you have them read Jon Skeet's threading series.


Since you are waiting on an I/O resource (the file download) you will not know if the thread is sleeping pending an I/O operation or if it is sleeping for another reason.


You can support progress reporting, incremental results and cancellation with events. Check out System.ComponentModel.BackgroundWorker

Edit: this is a shameless plug.
Look at 'Concurrent Programming on Windows' by Joe Duffy. It exaplains a lot of this stuff in detail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜