It is runDownloader()
that must be able to pause.
It will need to periodically call waitHandle.WaitOne()
on the wait handle.
Your WaitHandle must be a ManualResetEvent
, not an AutoResetEvent
and you should initialise it so that it is signalled (unless you want to start your thread in a "paused" state).
You will also have to change your button handlers as follows:
private void btnPause_Click(object sender, EventArgs e)
{
waitHandle.Reset(); // Need to pause the background thread
}
private void btnContinue_Click(object sender, EventArgs e)
{
waitHandle.Set(); // Need to continue the background thread
}
This means that you must be able to pass waitHandle to the thread so that it can wait on it.
However, there are better ways of managing thread cancellation since .Net 4, namely the use of CancellationTokenSource
and CancellationToken
.
See this Microsoft article for details.