DotnetCore thread termination problem, we use interrupts to solve


If you don’t want to read nonsense, use this method: Interrupt(); Link: docs.microsoft.com/zh-cn/dotne…


Today I found a problem when using dotnetCore multithreading.

Thread has no Abort() method.

See below:Only the.netCore does not support this member… Six periods dedicated to the true fragrance.



So what about termination for a lot of friends?

Since it can’t be stopped, let’s stop:

dangdangdangdangdangdang

:

Interrupt();

Use screenshots as follows :(execute screenshots interrupted after 6 seconds)

Official reference method docs.microsoft.com/zh-cn/dotne…

The rest will be released by the backstage. Remember to add

   thread.IsBackground=true;
Copy the code

Now hand over the complete core code:

using System; using System.Threading; namespace dotnetcoreThreadInterrupt { class Application { public void Start() { Thread thread = new Thread(TtoRun); thread.IsBackground=true; thread.Start(); for (int i = 0; i < 6; i++) { Thread.Sleep(1000); } thread.Interrupt(); } public void TtoRun() { while (true) { Thread.Sleep(1000); System.Console.WriteLine("W"); }}}}Copy the code

Gitbub link: github.com/daolizhe/do…

Results: remember is directory dotnetcoreThreadInterrupt dotnet directly run can use, if the version is wrong, make it another version