0

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.

sll
  • 61,540
  • 22
  • 104
  • 156
Maya
  • 989
  • 4
  • 12
  • 19

2 Answers2

0

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/

giammin
  • 18,620
  • 8
  • 71
  • 89
  • its not my design and i must use it like it is... anyway the problem that i run script with Microsoft.Scripting and for this i start new thread that call to Execute() and i want to be able to abort the script – Maya Mar 18 '12 at 10:13
0
  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.

Community
  • 1
  • 1
OSH
  • 2,847
  • 3
  • 25
  • 46
  • @maya do you have the option to use tasks (http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx) instead? Tasks will allow you to create child tasks that die when the parent dies (and also support cancellation token that will pretty much do all that you need) – OSH Mar 18 '12 at 10:29
  • Task will be perfect but i can't change it to task :-( – Maya Mar 18 '12 at 10:32
  • so, you don't own the code that created the inner task? I looked for a way to enumerate all the process' threads (so you enumerate before starting the script and hope all the new additions came from the script -- a very bad hack, but might work). Couldn't find a way to do that. I can't find any way of doing what you want unless you have some control over the inner code (the script code). – OSH Mar 18 '12 at 10:52