Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Backgroundworker Object's Thread Priority

I am trying to use the .NET Backgroundworker Object in an application I am developing.

All the material on the internet say that this object runs in the "background" but nowhere have I been able to confirm that this background thread indeed runs in a "low priority" mode. This question arises because in Windows (I assume) a background task can run in a 'normal' or 'below normal' or 'low' priority mode.

In my application, I tried to set the priority myself inside the DoWork function by calling ...

Thread.CurrentThread.Priority=ThreadPriority.Lowest

...

but this seems to have no effect. Does the backgroundworker ignore this call?

I would like to explain some more:

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

system.net.webclient.UploadValuesAsync(...) calls

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

...

Synclock objlock
    debug.print("Queueing reading...")
    dataQ.Enque(reading)
End Synclock
...

The backgroundworker's Dowork function dequeues and then uploads like so...

..............

Do
        if dataQ.count() <> 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

................

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!

Is there something wrong in my approach? Should I not be using the background-worker object at all?

BTW, this is in a dual-core laptop running Windows XP.

like image 209
Soundar Rajan Avatar asked Feb 07 '09 20:02

Soundar Rajan


People also ask

What is the thread in the backgroundworker?

The Thread part of the BackgroundWorker is sort of hidden from you. You get to work with two very important parts of the BackgroundWorker though, the DoWork and RunWorkerCompleted events (There's a progress one too, but because I actually don't use this much I'll omit it for you to take on as homework). So... What are these events all about?

Do background threads have lower priority than foreground threads?

Background threads do not have lower priority. The difference between foreground and background threads is that the CLR will shutdown the process once no more foreground threads are running.

How to create a thread with a specific priority?

If you need threads of a specific priority you should create them using the Thread type and set the priority on the instance as desired. Yes, something is wrong with your approach - you're basically tight looping when the queue is empty. Whatever the thread priorities, that's a bad idea.

What is the backgroundworker component?

The BackgroundWorker component was designed, in order to provide the. NET developer with a handy way to run code as a separate thread in GUI environments (i. e. Windows Forms apps), report progress, as well as to add handling code in the event of completion of the BackgroundWorker’s thread.


2 Answers

Just to add to what Jon and Marc have already said:

Background threads do not have lower priority. The difference between foreground and background threads is that the CLR will shutdown the process once no more foreground threads are running. Thread pool threads are background threads.

You can actually set the priority of a thread pool thread, but as you have next to no control of which thread pool thread will actually run your task it is not advisable to do so. If you need threads of a specific priority you should create them using the Thread type and set the priority on the instance as desired.

like image 66
Brian Rasmussen Avatar answered Oct 07 '22 01:10

Brian Rasmussen


Yes, something is wrong with your approach - you're basically tight looping when the queue is empty. Whatever the thread priorities, that's a bad idea.

There's nothing wrong with using a background worker for this, but the enqueuing/dequeuing should really just use a producer/consumer queue which blocks when you try to dequeue when there's nothing ready.

I have an example implementation of a producer/consumer queue in my threading tutorial - see about half way down the linked page. You'll want some way to tell the dequeuing process that it's finished, by the way. (For example, enqueuing a null reference or other special value.) That code was written pre-generics, but it should be easy to update.

like image 8
Jon Skeet Avatar answered Oct 06 '22 23:10

Jon Skeet