简体   繁体   中英

Starting new theard from another thread and abort it C#

if i do somtheing like this:

new Thread(DoWork).Start();

void DoWork(){new Thread(DoMoreWork).Start();}

and i aborting the first thread that run DoWork is DoMoreWork also will aborted? if not how can i abort the sec thread (DoMoreWork)?

Thanks!

Update:

The problem that I run script with Microsoft.Scripting and for this I start a new thread that call Execute() method from Microsot.Scripting and I want to be able to abort the script. I can abort my thread but I don't know which thread Execute create to run the script.

this is a bad design...

you should not do that because if the second thread hangs also the first will not close and your GC will not clean resources AKA deadlock

threads are meant to run in parallel and sync not in a hierarchy.

there you can find a good guide to .net threading: http://www.albahari.com/threading/

  1. Aborting the "parent" thread will not stop the "child".

  2. Using Thread.Abort is a bad idea. you should design your thread to exit gracefully: see the answers to this question for example.

If you design your threads to exit gracefully (for example when you signal an event), this will also allow you to kill both threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM