title: .NET Backgroundworker Object's Thread Priority
body: <p>I am trying to use the .NET Backgroundworker Object in an application I am developing.</p>
  
  <p>All the material on the internet say that this object runs in the "background" but <strong>nowhere have I been able to confirm that this background thread indeed runs in a "low priority" mode</strong>. This question arises because in Windows (I assume) a background task can run in a 'normal' or 'below normal' or 'low'  priority mode. </p>
  
  <p>In my application, I tried to set the priority myself inside the DoWork function by calling
  ...</p>
  
  <pre><code>Thread.CurrentThread.Priority=ThreadPriority.Lowest
  </code></pre>
  
  <p>...</p>
  
  <p>but this seems to have no effect. Does the backgroundworker ignore this call? </p>
  
  <p>I would like to explain some more:</p>
  
  <p>My application is an internet client that collects real-time data on temperature,humidity etc from a chamber and uploads to a web page (not a web service) using</p>
  
  <p><code>system.net.webclient.UploadValuesAsync(...)</code> calls</p>
  
  <p>I have written the application such that the client GUI collects the data from the chamber, time-stamps them and then queues them for upload like so</p>
  
  <p>...</p>
  
  <pre><code>Synclock objlock
      debug.print("Queueing reading...")
      dataQ.Enque(reading)
  End Synclock
  ...
  </code></pre>
  
  <p>The backgroundworker's Dowork function dequeues and then uploads like so...</p>
  
  <p>..............</p>
  
  <pre><code>Do
          if dataQ.count() &lt;&gt; 0 then
              Synclock objlock
                reading = dataQ.DeQue()
              End Synclock
              Dim client As New System.Net.WebClient
              ...put the reading in NameValueCollection and upload to server page
              req = new NameValueCollection
              ...
              ...
              client.UploadValuesAsync(uri, "POST", req)
          endif
          thread.sleep(1) 'without this line the CPU usage goes upto 100% and seems to slow other tasks!
      Loop
  </code></pre>
  
  <p>................</p>
  
  <p>When I run the program I find that whenever the UploadValuesAsync is called the print out the debug window stops. I had also added debug statements to see how many readings are in the queue at any time. It this task is truly run in a low priority, I expected to see the queue count increase rapidly as data is acquired and then decrease only when foreground is idle and data is not being acquired. But this is not the case. As soon as a reading is added to the queue it is dequeued and uploaded. So the queue count is always is either 1 or 0!</p>
  
  <p>Is there something wrong in my approach? Should I not be using the background-worker object at all?</p>
  
  <p>BTW, this is in a dual-core laptop running Windows XP.</p>