For pausing/resuming thread, use the class
System.Threading.EventWaitHandle
or
System.Threading.ManualResetEvent
(the behavior of these classes are exactly identical in case you use manual reset). In a thread, you use
System.Threading.EventWaitHandle.WaitOne
. This is a blocking code. If the event is reset (is in
nonsignaled state), it will switch out your downloading thread and put it in the wait state, that is, the thread won't be scheduled back to execution until awaken. So, the wait spends zero CPU time. The thread will be awaken by
Thread.Abort
or when the same instance
EventWaitHandle
is signaled, which you can do in some different (say, UI) thread, by calling
EventWaitHandle.Set
. This is your mechanism for suspending/resuming your thread.
Obsolete methods
Suspend/Resume
are unsafe and should be be used.
As to downloading, I would rather advise to use
System.Net.HttpWebRequest
. Please see:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[
^].
In contrast to other method, this class will allow you to download a file sequentially, piece by piece, so you can insert
EventWaitHand.WaitOne
between blocks. More high-level (less direct) methods based on events won't allow you to do so. For a complete code sample, please see my application HttpDownloader:
how to download a file from internet[
^].
Please see also my other past answers on the topic:
How to get the data from another site[
^],
get specific data from web page[
^].
—SA