C#:BackgroundWorker 线程优先级
我在 C# 中使用 BackgroundWorker,需要使用 AvSetMmThreadCharacteristics 将线程的优先级更改为 RealTime,将特性更改为“Pro Audio”。
由于BackgroundWorker回收ThreadPool中的线程,因此不建议更改这些类型的属性,但如果我必须重写代码以使用普通的Thread(),我就会失去所有额外的东西(取消/进度/等)。
那么,当坚持使用BackgroundWorker 线程时,我会遇到什么问题呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需创建一个单独的线程,您就拥有它。池线程的用途相对较短,这似乎不适合您的情况。
重现进度和取消逻辑并不那么困难。
当您更改 bgw 内的属性时,请确保将它们设置回来(尝试/最后)。但还有其他问题,当您锁定或等待某些内容时,您的托管池线程可能会被切换。至少规格并没有排除这种可能性。
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.