Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

BackgroundWorker with Thread.Sleep()

$
0
0

Hi,

I have a long-running task in BackgroundWorker's Dowork() as follows:

private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
          if (UDPSocket.Available > 0)
          {
            UDPSocket.Receive(p_ReceiveConsoleData);
          }

          Thread.Sleep(1);
        }

    }

When my application has about 20-30 threads and overall CPU utilization is higher, my BackgroundWorker stops at

Thread.Sleep(1);

When BackgroundWorker stops at above line, other worker threads keep working normally, only the BackgroundWorker calling Thread.Sleep() stops at Thread.Sleep(). His happens intermittently, once in a while.

Following is the screenshot of threads of dump:

--

-- 

Please help with following: 
1. why does the Sleep() call in BackgroundWorker freezes the particular BackgroundWorker thread?
2. I have while(1) loop in BackgroundWorker is calling Thread.Sleep() required to give execution time to other worker threads? If not, I'll remove Thread.Sleep() call.
3. Should a Timer object be used for long-running task or while loop? 

Thanks.


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>