. Net – how to stop a worker thread in a multithreaded windows service where the service is stopped

I have a Windows service that uses a producer / consumer queue model to process tasks in queues with multiple worker threads These tasks can run for a long time, if not a few hours, in the order of many minutes, without involving loops

My problem is the best way to handle service stops to gracefully end processing of these worker threads I've read another so question, using thread Abort () is a sign of bad design, but it seems that the service onstop () method can only complete for a limited time before the service terminates I can clean up enough in the catch of threadabortexception (there is no danger of inconsistent state), so I call thread Abort () seems good for me on worker threads Is it? What are alternatives?

Solution

Indeed, suspension should be avoided It's best to give them some time to exit gracefully - maybe consider stopping them after timeout - but in the end, service stopping can do this by killing the process

I will try to send a "flush and exit" signal in my queue - much like the "close" method here, but there are some signals when I finish

If you resort to suspension - think the process is fatal Kill it as soon as possible

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>