Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: BackgroundWorker Thread Priority

I'm using a BackgroundWorker in C# and I need to change the thread's priority to RealTime and characteristics to "Pro Audio" using AvSetMmThreadCharacteristics.

Since the BackgroundWorker recycles threads in the ThreadPool, it's not adviced to change these kind of properties, but if I have to rewrite the code to use a normal Thread() instead, I loose all the extras (cancellation/progress/etc).

So, what problems can I expect when stick to a BackgroundWorker thread?

like image 763
Maestro Avatar asked Apr 05 '26 21:04

Maestro


1 Answers

Just create a separate thread, you'll own it. A pool thread is intended for relatively short use, that doesn't seem to fit your case.
Reproducing the progress and cancellation logic isn't so difficult.

When you do change the properties inside a bgw, make sure you set them back (try/finally). But there are other issues, your Managed Pool thread might be switched when you lock or wait on something. At least the specs don't rule that out.

like image 114
Henk Holterman Avatar answered Apr 08 '26 12:04

Henk Holterman