0

Let's say we have code:

var thread = new Thread(() => Native.SomeMethod());
thread.Start();

Here Native.SomeMethod - is long lasting operation implemented in C++. How to terminate thread instantly? thread.abort() not helping in this case.

  • why do you start the thread when you want to terminate it instantly? – 463035818_is_not_an_ai Aug 07 '17 at 12:53
  • @tobi303 thread can be working for some time before terminating moment. The point is at that moment I need the way to free CPU resources instantly. –  Aug 07 '17 at 12:57
  • @tobi303 he probably just wan't to terminate the thread instantly at any moment, not necessarily right after starting it. – Jabberwocky Aug 07 '17 at 13:01
  • [What's wrong with using Thread.Abort()](https://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort) discussion might be helpful. – Leonid Vasilev Aug 07 '17 at 14:28

2 Answers2

0

Assuming that all is OK in running the Native method to end the thread, the Abort method I think should work albeit using it has some caveats. As far as I know, it works and is capable of ending a blocked thread but not sure if it works on nonblocked threads.

If I understand your intention correctly you want to instantly end the thread. Have you thought of background running? Just a thought ... But if you want to end the thread my thoughts on that is as follows.

In C# you have two methods to end Threads. Interrupt and Abort.

Abort is normally used to forcefully release a blocked thread. It throws ThreadAbortException which you need to handle in the catch block. But note that it is again rethrown at the end of the catch block as it attempts to terminate the thread. To avoid this call Thread.ResetAbort within the catch block.

In your case I think it is worth trying something like the following... (did not try it it might not work but worth trying)

 Thread thread = new Thread(() =>   try{ 

      Native.SomeMethod();
     //if this does not work add a blocking statement here  like 
    //Thread.Sleep(time in mls such as 2000)
      catch(ThreadAbortException){
       Thread.ResetAbort;
    }

);
thread.Start();

The other method is Interrupt.

Interrupt does not immediately end a thread that is not blocked unless the ThreadInturruptException (similar to ThreadAbort Exception) is unhandled. And this happens until next blocks, at a point where ThreadInterruptException is unhandled.

Note that both Interrupt and Abort have caveats. For more on this google threading in C# by Joseph Albahri and give it a read.

If the Thread Abort does not work for you may be trying Safe Cancellation Tokens is the other option you have. Even though this might be a bit involving and requires you to change your design a bit. But if you know your way around cancellation tokens it is a good idea to give them a try.

Kumssa wakoo
  • 81
  • 1
  • 5
  • I have some strange behavior when calling Abort on thread with native code - massive SEH exceptions and other bad things. I think it's because of interaction logic between managed and unmanaged code. –  Aug 07 '17 at 14:50
0

Thread.Abort() throws a ThreadAbortException that only works for .NET code. If you want to interop between threads in managed and native code, you should implement some Kind of protocol to terminate the thread more gracefully, e.g. by using a shared volatile bool variable that is used as a stopping flag.

See also Question about terminating a thread cleanly in .NET

EFrank
  • 1,880
  • 1
  • 25
  • 33