1

Is there any alternative option for thread.Abort() in asp.net core as it no longer support with .Core.

        //   Exceptions:
        //   T:System.PlatformNotSupportedException:
        //   .NET Core only: This member is not supported.

This is throw PlatformNotSupportedException exception.

I used thread.Interrupt() but it not work as per expectation.

Raju Paladiya
  • 778
  • 2
  • 12
  • 35
  • 2
    What are you trying to do? You didn't need Thread.Abort in web applications built on top of the Full framework in the first place. It was a bad idea there, especially since Tasks and cancellation tokens were introduced in 2010. – Panagiotis Kanavos Feb 06 '19 at 13:12
  • 2
    You most *definitely* shouldn't use `Thread.Abort()` in ASP.NET, where the threads are used to handle multiple requests – Panagiotis Kanavos Feb 06 '19 at 13:15

2 Answers2

5

Thread.Abort() has been removed, since it could not be reliably ported to all platforms and in general poses a threat to the entire app domain, when used incorrectly.

The recommended way (especially for ASP.NET Core) is now to use Tasks and "abort" them with CancellationTokens. You can learn more about how to use CancellationTokens here.

Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
  • Do you have a source for the above reasons for the removal of Thread.Abort? Specifically: - Is this Microsoft's published rationale behind the decision ? I'm not after the statement of "we removed it" - but more the details of why it was removed. The internal reasons. - Why is the entire app domain "posed a threat" by a thread abort ? I can understand in constrained devices that this would be an issue though this can be handled via throwing an exception only in those specific environments. Is this a case of throwing the baby (all platforms) out with the bathwater (some constrained devices)? – David Ford Oct 21 '20 at 06:05
  • Here's the explanation from Microsoft: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/thread-abort-obsolete – oderibas Nov 07 '21 at 22:01
0

A solution here could be to use a shared variable which unless set to false will allow the thread to continue, like tickRunning to control the loop in the below code example:

using System;
using System.Threading;

namespace SharedFlagVariable
{
    class Program
    {
        static volatile bool tickRunning; // flag variable
        static void Main(string[] args)
        {
            tickRunning = true;
            Thread tickThread = new Thread(() =>
            {
                while (tickRunning)
                {
                    Console.WriteLine("Tick");
                    Thread.Sleep(1000);
                }
            });
            tickThread.Start();
            Console.WriteLine("Press a key to stop the clock");
            Console.ReadKey();
            tickRunning = false;
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
    }
}

P.S. If you prefer to use System.Threading.Task library then check out this post explaining how to Cancel Task using CancellationToken approach.

dave004
  • 3
  • 2
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
  • Downvoter please explain the reason for downvoting, it's not polite to downvote just because you might not like the solution. – Arsen Khachaturyan Jan 29 '20 at 15:32
  • 2
    Your solution works in a simple scenario like you're showing in your example, but it is generally not good advice for people building more complex applications. Your variable is not thread-safe and even if it was marked as `volatile` it would still lack in comparison to Task/CancellationToken. It is nice that you mentioned them, but it isn't really a 'if you prefer to - use them', it is 'you should be using them'. Also you rarely need to or should create your own Threads, especially considering the OP asked about ASP.NET Core. – Tobias Tengler Jan 31 '20 at 11:56
  • 2
    I got your point, but people not always look for very complex solutions, sometimes the solution can be very simple, or the post can give you just a basic idea of how to do the things, and of course you are welcome to enhance it further to make more elegant. Also, it's worth mentioning that I've also suggested an alternative solution that could be handy for some people. – Arsen Khachaturyan Jan 31 '20 at 18:43