8

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). And since I 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 check 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.

Alex
  • 13,024
  • 33
  • 62
Juan
  • 15,274
  • 23
  • 105
  • 187

3 Answers3

4

Sounds to me you are busy-waiting with a while loop. You should use event signaling instead, eg. a WaitHandle. A busy-waiting loop in release mode might very well consume all your cpu, giving a feeling of a freeze.

Signal the WaitHandle in DownloadStringCompleted or if the user cancels the download.

Check the MSDN docs on the WaitHandle class. There's also an example there.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79
  • Pause/Resume loop in Background worker http://stackoverflow.com/questions/8359058/pause-resume-loop-in-background-worker – Steven Du Dec 31 '12 at 01:40
0

Send your while loop that is checking for the cancelation to sleep for a short while (some ms). This well free up the CPU runtime for other threads and processes.

Mark
  • 7,891
  • 5
  • 27
  • 36
  • 1
    Busy waiting is bad practice. – Mikael Svenson Jun 12 '10 at 08:35
  • I know and I didn't know that there was something like a WaitHandle in C#/.Net (always new things to learn :-)), but sometimes polling is the only solution and then one should make sure that the process polling only checks once and then gives up the CPU time (in Linux its the yield() function) given to the thread/process by the OS. – Mark Jun 12 '10 at 08:43
  • The WaitHandles can also be used with a timeout, so that would prevent polling :) (And I have used my share of busy waiting until I got wiser.. hehe) – Mikael Svenson Jun 12 '10 at 09:43
0

nice topic, i used In my background worker process a double while

 int icounter = 1;
 while (icounter < SomeListInventory.Count)
            {
                while (pauseWorker == false)
                { 
                    //-----------------------
                    //DO SOME WORK
                        icounter++;
                    //-----------------------
                }
            }

And i have a button pause, that when i press it, pauseWorker (Global variable or property) becomes true and loops in the first while only, without increasing the icounter, and when i make the pauseworker=false again the process continues

themhz
  • 8,335
  • 21
  • 84
  • 109