Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find Abort method in Thread

I'm porting a project to .NET Core and have some logic with the method. Do you know .NET Core supports Abort() method in Thread class?

like image 688
Serhii Shemshur Avatar asked Aug 31 '16 07:08

Serhii Shemshur


People also ask

How do you use thread abortion?

If Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code. If two calls to Abort come at the same time, it is possible for one call to set the state information and the other call to execute the Abort .

What will happen if the abort method is called on a thread which has not been started while working in C#?

If the Abort method is called on a thread which has not been started, then that thread will abort when Start is called. If the Abort method is called on a thread which is blocked or is sleeping then the thread will get interrupted and after that get aborted.

What is thread abort exception?

ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before ending the thread.

What is the exception that will be thrown if you try to start an aborted thread using start method?

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.


2 Answers

Thread.Abort has been removed in .NET Core.

  • Move Thread.Abort under FEATURE_LEGACYSURFACE https://github.com/dotnet/coreclr/pull/2342
  • Never call Thread.Abort https://github.com/aspnet/KestrelHttpServer/pull/726
  • for tests purpose the custom Abort Ext method is used
like image 108
Set Avatar answered Sep 23 '22 06:09

Set


As far as I have understood so far it is now recommended to use CancellationToken. Please read more about how to use this here:

https://msdn.microsoft.com/en-us/library/dd997364(v=vs.110).aspx

like image 22
Danny van der Kraan Avatar answered Sep 21 '22 06:09

Danny van der Kraan