.NET thread interruption

I create thread A and in this thread create new thread B.

So how is the hierarchy of threads? Is Thread B a child of Thread A? Or are threads created as peers?

I want to interrupt the parent thread A, which in turn kills / interrupts the child threads.

How is this possible in C #?

+3
source share
5 answers

Threads should ideally never be interrupted. It is simply unsafe. Consider this as a way to suppress an already sick process. Otherwise, avoid the plague.

A better way to do this is to have something that the code can periodically check, and decided to exit.

+11
source

An example of stopping threads in a polite way:

using System;
using System.Threading;

namespace Treading
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main program starts");
            Thread firstThread = new Thread(A);
            ThreadStateMessage messageToA = new ThreadStateMessage(){YouShouldStopNow = false};
            firstThread.Start(messageToA);
            Thread.Sleep(50); //Let other threads do their thing for 0.05 seconds
            Console.WriteLine("Sending stop signal from main program!");
            messageToA.YouShouldStopNow = true;
            firstThread.Join();
            Console.WriteLine("Main program ends - press any key to exit");
            Console.Read();//
        }

        private class ThreadStateMessage
        {
            public bool YouShouldStopNow = false; //this assignment is not really needed, since default value is false
        }

        public static void A(object param)
        {
            ThreadStateMessage myMessage = (ThreadStateMessage)param;
            Console.WriteLine("Hello from A");
            ThreadStateMessage messageToB = new ThreadStateMessage();
            Thread secondThread = new Thread(B);
            secondThread.Start(messageToB);

            while (!myMessage.YouShouldStopNow)
            {
                Thread.Sleep(10);
                Console.WriteLine("A is still running");
            }

            Console.WriteLine("Sending stop signal from A!");
            messageToB.YouShouldStopNow = true;
            secondThread.Join();

            Console.WriteLine("Goodbye from A");
        }

        public static void B(object param)
        {
            ThreadStateMessage myMessage = (ThreadStateMessage)param;
            Console.WriteLine("Hello from B");
            while(!myMessage.YouShouldStopNow)
            {
                Thread.Sleep(10);
                Console.WriteLine("B is still running");
            }
            Console.WriteLine("Goodbye from B");
        }
    }
}

Thread.Abort(); , . , , . , .

+6

Thread.Abort , , , , ,

+4

, :

, , , , . "". . , , 100% , , 0, .

var dieEvent = new AutoResetEvent(false);
int slaveRestPeriod = 20;// let not hog the CPU with an endless loop
var master = new Thread(() =>
                            {
                                doStuffAMasterDoes(); // long running operation
                                dieEvent.Set(); // kill the slave
                            });

var slave = new Thread(() =>
                           {
                               while (!dieEvent.WaitOne(restPeriod))
                               {
                                   doStuffASlaveDoes();
                               }
                           });

slave.Start();
master.Start();
+3

, Thread A, ThreadA.Abort() . , false, .

public class MyClass
{
    public static Thread ThreadA;
    public static Thread ThreadB;

    private void RunThings()
    {
        ThreadA = new Thread(new ThreadStart(ThreadAWork));
        ThreadB = new Thread(new ThreadStart(ThreadBWork));

        ThreadA.Start();
        ThreadB.Start();
    }

    static void ThreadAWork()
    {
        // do some stuff

        // thread A will close now, all work is done.
    }


    static void ThreadBWork()
    {
        // do some stuff

        ThreadA.Abort(); // close thread A

        // thread B will close now, all work is done.
    }
}
+1

Source: https://habr.com/ru/post/1751514/


All Articles