Abort is not supported on this platform. Need guidence. How to solve this issue? i am working on splash screen.

sherry leo 1 Reputation point
2022-01-14T00:15:03.773+00:00

165001-hel.jpg

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

4 answers

Sort by: Most helpful
  1. Pouya Nozari Asl 0 Reputation points
    2026-08-15T07:43:53.13+00:00

    It is, now, technically possible to use Thread.Abort on .NET!

    Download this NuGet package: https://www.nuget.org/packages/NET-Thread.Terminate -

    add it to your project. However, this is not a recommended approach on .NET anymore. Your code shouldn't ever reach the point where you lose the control of your thread in an ideal production scenario: to end a thread in production code, prefer cooperative cancellation such as CancellationToken. Nonetheless, if you still insist on it:

    You can abort the thread by calling the extension method DotNetAbort:

    SomeThread.DotNetAbort(); // The DotNetAbort method may corrupt the process and should not be used in production code.
    

    This is the same as the removed Thread.Abort method; however, There is a catch you when aborting the thread on .NET 5 and .NET 6 using this method: a random exception might be thrown at the thread before the main ThreadAbortException exception; this possible case must be handled like the code below:

        Thread SomeThread = new Thread(() =>
      {
          Exception exception = null;
          try
          {
              try
              {
                  for (; ; Thread.Sleep(150))
                  {
                      Console.WriteLine("Stop me!");
                  }
              }
              catch (Exception ex)
              {
                  exception = ex;
              }
          }
          catch (ThreadAbortException ex)
          {
              exception = ex;
              // Use ResetAbort() to reset the abortion when using DotNetAbort methods; not Thread.ResetAbort()! 
              Thread.CurrentThread.ResetAbort();
          }
          if (exception != null)
          {    
              // doing finalizing stuff
    
              if (exception.GetType() == typeof(ThreadAbortException))
              {
                 
                  return;
              }
              Console.WriteLine($"An error occurred. {exception.Message}");
          }
      })
      { Name = ".NET Thread", IsBackground = true };
    
      SomeThread.Start();
      SomeThread.DotNetAbort();
    

    This only needs to be done on .NET 5 and .NET 6; starting with .NET 7, this isn't necessary, so just handle it the way you would before.

    You could also terminate the .NET on an OS level the way you do a native thread by calling the extension method Terminate:

    SomeThread.Terminate(); // Calling this method might cause corruption in the runtime; avoid practicing usage in an ideal production scenario
    

    Was this answer helpful?

    3 people found this answer helpful.
  2. sherry leo 1 Reputation point
    2022-01-14T11:56:23.947+00:00

    165076-hel.jpg

    I am working like this i have tried join and timer but it didn't work and i have tried cancelation token too but cancelation token is not accepting splash screen. Any other alternative to start a form until the second form loads and then close it in .net5?

    Was this answer helpful?

  3. Jack J Jun 25,306 Reputation points
    2022-01-14T06:14:18.897+00:00

    @sherry leo , as the Microsoft article said, Thread.Abort is obsolete.

    Previously, calls to Thread.Abort did not produce compile-time warnings, however, the method did throw a PlatformNotSupportedException at run time.

    You can refer to the following link to use CancellationToken and suppress the compile-time warning to try to solve your problem.

    Recommended action


    If the response is helpful, please click "Accept Answer" and upvote it.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments
  4. P a u l 10,766 Reputation points
    2022-01-14T00:45:38.49+00:00

    The cleanest way to stop a thread is by stopping them cooperatively, i.e. you pass an object to your thread to communicate that you want it to terminate. You can create one CancellationToken and pass it to any third-party calls you're executing in your thread:
    https://learn.microsoft.com/en-us/dotnet/standard/threading/using-threads-and-threading#how-to-stop-a-thread
    https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads#code-example

    Since .NET 5 the Thread.Abort method throws this exception, so either the thread needs to be cancelled or it needs to be ran as part of a separate process. If you use a separate process you can call Process.Kill to force it to terminate, although that may not be ideal depending on your application.

    Was this answer helpful?

    0 comments No comments