SYSLIB0006: Thread.Abort is not supported
The following APIs are marked obsolete, starting in .NET 5. Use of these APIs generates warning SYSLIB0006
at compile time and a PlatformNotSupportedException at run time.
When you call Thread.Abort to abort a thread other than the current thread, you don't know what code has executed or failed to execute when the ThreadAbortException is thrown. You also cannot be certain of the state of your application or any application and user state that it's responsible for preserving. For example, calling Thread.Abort may prevent the execution of static constructors or the release of managed or unmanaged resources. For this reason, Thread.Abort always throws a PlatformNotSupportedException on .NET Core and .NET 5+.
Workarounds
Use a CancellationToken to abort processing of a unit of work instead of calling Thread.Abort. The following example illustrates the use of CancellationToken.
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);
}
}
Suppress a warning
If you must use the obsolete APIs, you can suppress the warning in code or in your project file.
To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.
// Disable the warning.
#pragma warning disable SYSLIB0006
// Code that uses obsolete API.
// ...
// Re-enable the warning.
#pragma warning restore SYSLIB0006
To suppress all the SYSLIB0006
warnings in your project, add a <NoWarn>
property to your project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
<NoWarn>$(NoWarn);SYSLIB0006</NoWarn>
</PropertyGroup>
</Project>
For more information, see Suppress warnings.