Skip to content

Thread.Abort is not supported and throws PlatformNotSupportedException on .NET 6 #65556

Closed

Description

@sasingh04-fadv

Description

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

Actual behavior

Abort the process.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Feb 18, 2022
  2. svick commented on Feb 18, 2022

    @svick
    Member

    The documentation for the warning you're getting suggests using CancellationToken instead. Though that is not a direct replacement and requires you to change the structure of the code.

  3. teo-tsirpanis commented on Feb 18, 2022

    @teo-tsirpanis
    Contributor

    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.

    But either way your code needs some refactoring.

  4. ghost locked as resolved and limited conversation to collaborators on Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Threadinguntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions