You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Thread.Abort is not supported and throws PlatformNotSupportedException on .NET 6 #65556
we are using the Thread.Abort on .Net 4.5. but now we have migrated code on .Net 6.0 and its giving the error not supported and throws PlatformNotSupportedException . is there any workaround on .net 6 to resolve this issue. Pls. let us know.
Reproduction Steps
using System;
using System.Threading;
class Test
{
public static void Main()
{
Thread newThread = new Thread(new ThreadStart(TestMethod));
newThread.Start();
Thread.Sleep(1000);
// Abort newThread.
Console.WriteLine("Main aborting new thread.");
newThread.Abort("Information from Main.");
// Wait for the thread to terminate.
newThread.Join();
Console.WriteLine("New thread terminated - Main exiting.");
}
static void TestMethod()
{
try
{
while(true)
{
Console.WriteLine("New thread running.");
Thread.Sleep(1000);
}
}
catch(ThreadAbortException abortException)
{
Console.WriteLine((string)abortException.ExceptionState);
}
}
}
Expected behavior
This method throws a [PlatformNotSupportedException
Hello @rrathord1885, aborting threads without asking them is a very dangerous practice and has not been supported by design in modern .NET since .NET Core 1.0, which is why I will close this issue.
As @svick said, the safe alternative is to pass CancellationTokens around in your thread and mark the points that are OK to abort, yourself by calling CancellationToken.ThrowIfCancellationRequested. A nice rule of thumb is to call this method at the beginning of a loop, but perhaps not all loops, you have to make a balance; if you call it too often performance will decrease, and if you call it too rarely code might not stop immediately.
If the thread you want to abort is performing I/O, you can cancel it by switching to asynchronous methods and the async and await keywords and passing to them that CancellationToken.
Activity
svick commentedon Feb 18, 2022
The documentation for the warning you're getting suggests using
CancellationTokeninstead. Though that is not a direct replacement and requires you to change the structure of the code.teo-tsirpanis commentedon Feb 18, 2022
Hello @rrathord1885, aborting threads without asking them is a very dangerous practice and has not been supported by design in modern .NET since .NET Core 1.0, which is why I will close this issue.
As @svick said, the safe alternative is to pass
CancellationTokensaround in your thread and mark the points that are OK to abort, yourself by callingCancellationToken.ThrowIfCancellationRequested. A nice rule of thumb is to call this method at the beginning of a loop, but perhaps not all loops, you have to make a balance; if you call it too often performance will decrease, and if you call it too rarely code might not stop immediately.If the thread you want to abort is performing I/O, you can cancel it by switching to asynchronous methods and the
asyncandawaitkeywords and passing to them thatCancellationToken.But either way your code needs some refactoring.