A parent has several child threads.
If user click on stop button the parent thread should be killed with 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.
You definitely do not want to kill any threads here, since (among other reasons) the “child threads” in question are all from the thread pool.
See this article on how to create and terminate threads.
In your case, you have multiple threads all working the
startParsingmethod. Assuming this method has a loop in it, you would create a class-levelboolcalled_stillWorkingor something, and set it totrueat the beginning of thestartWorkingmethod.Inside the loop in
startParsing, you check that_stillWorkingistrueeach time through. To “cancel” all these threads, then, you just set_stillWorkingtofalseand wait for the threads to finish.