'What is correct way to abort a un-managed thread in C#
I'm working for some AOI framework. In our case, all threads must stop immediatly when user click the emergency button. The thread's job is calling some heavy cpu loading in un-managed c++ dlls.
As far as i know, .net is not recommand to abort any thread or task. So is there any wait to kill the thread in un-managed dll?
Solution 1:[1]
If killing jobs is crucial to the workflow, put each "killable" unit of work in a separate process, which you can then kill much more easily using Process.Kill().
Why? Because threads are relatively hard to manage, especially stop/kill them, especially when you don't control the code that runs on them.
Why is that? Because the ways to kill threads are:
- Cooperatively: Signal the thread to stop, then wait for the thread's code to see the signal and exit. That's how Task Canellation works. CON: If the code doesn't check for cancellation during long/block operations, the cancel will not happen. And if you don't control the code, you can't add that.
- Pre-emptively: Using
Thread.Abort()or its unmanaged equivalent. There are many issues with it, see What's wrong with using Thread.Abort()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
