-2

I'm implementing the Threading concept in that we need to call more then one method simultaneous so for we used

Parallel.Invoke(
    () => { GetLink(words); },
    () => { GetSA(words); },
    () => { Getlp(words); });

This case 1 working fine but. In case 2 we need to call three method and i need to find which method execute first and it has the result in the first method and other 2 method are need to kill or stop execute.it applicable for all the 3 method.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 3
    If one method requires the results of another method, perhaps the parallelism of this is not for you. – Moo-Juice Dec 04 '13 at 16:12
  • There is already a lot of information about this particular problem on StackOverflow and on the web in general. No need to ask another duplicate question. – Roman Polunin Dec 04 '13 at 16:23

1 Answers1

1

First, read this existing answer: C# Thread Termination and Thread.Abort()

Second, read this another one: What's wrong with using Thread.Abort()

General recommendation is 'never kill or abort a thread'. There are multiple reasons for this, however it is better to read detailed explanations on the web rather then re-post them here every time.

Instead of aborting/killing, modify your code so that sub-tasks can stop themselves gracefully.

You can have a shared CancellationToken (see CancellationTokenSource class), and check its status periodically in every thread's loop(s) - or use it in 'WaitAny' methods. All threads will set this token at the time when they finish, so the first one to complete will send signal to others to stop.

If you don't have time or willingness to implement graceful stopping, create and run Threads explicitly, Parallel and Tasks are designed for a safer programming style.

On the Thread, you can call Abort method that will throw ThreadAbortException inside the Thread.

Community
  • 1
  • 1
Roman Polunin
  • 53
  • 3
  • 12