.NET Backgroundworker Object's Thread Priority

杀马特。学长 韩版系。学妹 提交于 2019-11-30 04:01:05

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.

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.

It doesn't claim to be low priority - background means a: not the UI thread, and b: it won't keep a process alive. In reality, it probably relates to ThreadPool threads.

If you want a specific priority thread, then use your own Thread object - but I wouldn't recommend even this normally...

Additionally - "background" doesn't mean "when idle". Even on a single core machine, you will probably see both threads get as much rnutime (if they want it). Even more so on multi-core.

You might want to look at this worker thread implementation. It has protected constructors for specifying the name of the thread, the priority of the thread and whether or not the thread is a background thread.

http://devpinoy.org/blogs/jakelite/archive/2008/12/20/threading-patterns-the-worker-thread-pattern.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!