How can I pause a BackgroundWorker? Or something similar...

Posted by juan on Stack Overflow See other posts from Stack Overflow or by juan
Published on 2010-06-12T08:27:44Z Indexed on 2010/06/12 8:32 UTC
Read the original article Hit count: 388

Filed under:
|

I was using a BackgroundWorker to download some web sites by calling WebClient.DownloadString inside a loop. I wanted the option for the user to cancel in the middle of downloading stuff, so I called CancelAsync whenever I found that CancellationPending was on in the middle of the loop. But now I noticed that the function DownloadString kinda freezes sometimes, so I decided to use DownloadStringAsync instead (all this inside the other thread created with BackgroundWorker). An since and don't want to rewrite my whole code by having to exit the loop and the function after calling DownloadStringAsync, I made a while loop right after calling it that does nothing but checks for a variable bool Stop that I turn true either when the DownloadStringCompleted event handler is called or when the user request to cancel the operation. Now, the weird thing is that it works fine on the debug version, but on the release one, the program freezes in the while loop like if it were the main thread.

© Stack Overflow or respective owner

Related posts about c#

Related posts about backgroundworker