views:

88

answers:

3

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 sleeping and will terminate VS in progress of a download?

+1  A: 

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.

jheddings
+1  A: 

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.

Chris Bednarski
+3  A: 

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.

jasonh
Create a status mechanism. That will work. Since the main thread will read it only would i be able to simply have an int status and read it when i need to? Terminate the thread on idle and leave it alone on DLing? But then to terminate i would require thread.abort. So i should make it the other way around and have all plugins read int status {get;} and ask the authors to check this.
acidzombie24
I would actually create an `Enum` for your status enumeration. Don't use `Thread.Abort`, instead have the interface require that the plugin provide a `Stop` method and have the author handle a graceful stop using the threading articles Jon Skeet wrote.
jasonh