Thread.Abort is now obsolete
The Thread.Abort APIs are now obsolete. Code targeting .NET 5.0+ will receive compile-time warnings when these methods are called. If the warning is suppressed and these methods are called, they will throw PlatformNotSupportedException at runtime.
Version introduced
.NET 5.0 RC1
Old behavior
Calls to Thread.Abort would not produce compile-time warnings. However, on all versions of .NET Core, they would throw exceptions at runtime.
void DoAbortThread()
{
// Below call always throws PlatformNotSupportedException on
// .NET Core 2.x - 3.x.
Thread.CurrentThread.Abort();
}
New behavior
Beginning with .NET 5.0, Thread.Abort is marked obsolete as warning. The runtime implementation of the method is unchanged: it continues to throw PlatformNotSupportedException.
void DoAbortThread()
{
// Below call produces compiler warning SYSLIB0006.
Thread.CurrentThread.Abort();
}
Only code targeting .NET 5.0 or later will see the compile-time warning. Code targeting earlier versions (like netcoreapp3.1 or netstandard2.0) will not see a compile-time warning.
Reason for change
Thread.Abort always throws PlatformNotSupportedException on all .NET Core and .NET 5.0+ runtimes. We annotated the API with [Obsolete] to help draw attention to this so that developers can remove these call sites from their own code bases.
Recommended action
If you are trying to abort processing a unit of work, consider using CancellationToken, as demonstrated below.
/*
* Don't do this.
*/
void ProcessPendingWorkItemsOld()
{
if (QueryIsMoreWorkPending())
{
if (QueryTimeoutReached())
{
// line below produces warning SYSLIB0006
Thread.CurrentThread.Abort();
}
WorkItem work = DequeueWorkItem();
ProcessWorkItem(work);
}
}
/*
* Do this instead.
*/
void ProcessPendingWorkItemsNew(CancellationToken cancellationToken)
{
if (QueryIsMoreWorkPending())
{
// If the CancellationToken is marked as "needs to cancel",
// this will throw the appropriate exception.
cancellationToken.ThrowIfCancellationRequested();
WorkItem work = DequeueWorkItem();
ProcessWorkItem(work);
}
}
For more information on using CancellationToken to support graceful cancellation, see the document "Cancellation in Managed Threads".
If you need to suppress this warning in code, you can do so by suppressing the warning code SYSLIB0006. This warning code is specific to Thread.Abort and will not suppress other obsoletion warnings in your code. We recommend removing such call sites from your own code.
The below samples only suppress the compile-time warning. They do not change the runtime implementation. Thread.Abort will still throw PlatformNotSupportedException at runtime.
void MyMethod()
{
#pragma warning disable SYSLIB0006 // suppress Thread.Abort compiler warnings
Thread.CurrentThread.Abort();
#pragma warning restore SYSLIB0006
}
This can also be suppressed project-wide via the .csproj.
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<!-- Disable "Thread.Abort is obsolete" warnings for entire project -->
<NoWarn>$(NoWarn);SYSLIB0006</NoWarn>
</PropertyGroup>
Category
Affected APIs
Issue metadata
- Issue type: breaking-change
Thread.Abort is now obsolete
The
Thread.AbortAPIs are now obsolete. Code targeting .NET 5.0+ will receive compile-time warnings when these methods are called. If the warning is suppressed and these methods are called, they will throwPlatformNotSupportedExceptionat runtime.Version introduced
.NET 5.0 RC1
Old behavior
Calls to
Thread.Abortwould not produce compile-time warnings. However, on all versions of .NET Core, they would throw exceptions at runtime.New behavior
Beginning with .NET 5.0,
Thread.Abortis marked obsolete as warning. The runtime implementation of the method is unchanged: it continues to throwPlatformNotSupportedException.Reason for change
Thread.Abortalways throwsPlatformNotSupportedExceptionon all .NET Core and .NET 5.0+ runtimes. We annotated the API with[Obsolete]to help draw attention to this so that developers can remove these call sites from their own code bases.Recommended action
If you are trying to abort processing a unit of work, consider using
CancellationToken, as demonstrated below.For more information on using
CancellationTokento support graceful cancellation, see the document "Cancellation in Managed Threads".If you need to suppress this warning in code, you can do so by suppressing the warning code
SYSLIB0006. This warning code is specific toThread.Abortand will not suppress other obsoletion warnings in your code. We recommend removing such call sites from your own code.This can also be suppressed project-wide via the .csproj.
Category
Affected APIs
System.Threading.Thread.Abort(all overloads)Issue metadata