C # How to kill parent thread

A parent has several child threads.

If the user clicks the stop button, the parent thread must be killed by all child threads.

//calls a main thread           
 mainThread = new Thread(new ThreadStart(startWorking));
 mainThread.Start(); 
////////////////////////////////////////////////
   startWorking()
{
    ManualResetEventInstance                    =   new ManualResetEvent(false);
    ThreadPool.SetMaxThreads(m_ThreadPoolLimit, m_ThreadPoolLimit);

    for(int i = 0; i < list.count ; i++)
   {        

        ThreadData obj_ThreadData   =   new ThreadData();
        obj_ThreadData.name      =   list[i];

        m_ThreadCount++;             

        //execute 
        WaitCallback obj_waitCallBack = new WaitCallback(startParsing);

        ThreadPool.QueueUserWorkItem(obj_waitCallBack, obj_ThreadData);
    }
  ManualResetEventInstance.WaitOne();
}

I want to kill mainThread.

+3
source share
3 answers

You definitely don't want to kill any threads here, since (among other reasons) the "child threads" are in question from the thread pool.

See this article on how to create and terminate threads.

, startParsing. , , bool _stillWorking - , true startWorking.

startParsing , _stillWorking true . "" , _stillWorking false .

+2

: (., , ). .

, , , IPC, .

, .NET API . .

+9

You might consider passing a request to exit the child stream back to the parent and then access it. I think you will find it a lot easier.

+2
source

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


All Articles