BackgroundWorker...

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay, this could be a general question, but then again the BW is a "Forms Component" so *shrug*.

Anyway, i'm curious as to:
  • Is the BackgroundWorker Class actually a "Thread" or something like that or is it something different. (It uses AsyncOperation, but I haven't seen anything like Threading.Thread in it's disassembly)
  • If it is using Threads, can I in some way (via inheritance or something) affect it's Thread Priority ?

Thanks
 
RunWorkerAsync does a delegate.BeginInvoke, which means "The target method will be called on a thread from the thread pool.", so it's running on a managed thread for which you can set priority from within RunWorkerAsync by setting Threading.Thread.CurrentThread.Priority.
 
Awesome.

I was curious, in Delphi, the Thread object (an abstract class with the Execute method being the abstract method) contains a method called Synchronize() which allows for the synchronization of data access between the worker thread and the main application thread. Useful in some circumstances when it necessary to access data outside of the current thread.

In VB.Net there is the Threading.Thread class, and the ThreadPool, along with the BackgroundWorker component class which acts as a wrapper around the access to the thread pool. However, I have not found anything that correlates to the Synchronize() method. Is there one?

Thanks
 
Control.Invoke lets you invoke a method (by delegate) on the thread that control belongs to. The BGW/AsyncOperation basically does the same, only via Threading.SynchronizationContext.
 
Please advise where exactly you can change the backgroundworker thread priority. Is this in the DoWork event once RunWorkerAsync is called? If so I believe the priority can only be changed at the time the tread is created and not afterwards. Have you got a simple example to change the priority of the background worker thread? Thanks

RunWorkerAsync does a delegate.BeginInvoke, which means "The target method will be called on a thread from the thread pool.", so it's running on a managed thread for which you can set priority from within RunWorkerAsync by setting Threading.Thread.CurrentThread.Priority.
 
Yes, DoWork is the RunWorkerAsync callback, so from that thread you can set Priority using the code I posted.
Threading.Thread.CurrentThread.Priority
You can find documentation and a code example using the Priority property here: Priority Property

Priority can be set anytime during execution of the thread, and can be evalutated by OS thread management also anytime.

A note of warning, threadpool threads has the primary purpose of reuse, so any kind of async call may use that thread after you finished with it, thus you should make sure to put the thread back to its default priority state before letting go of it.
 
Back
Top