Alex
Alex

Reputation: 11137

Thread stopping

Is stopping a thread by acting the "break" command at some point a safe way to stop a thread?

Upvotes: 2

Views: 3147

Answers (3)

Phil Lamb
Phil Lamb

Reputation: 1437

Do you mean doing something like this?

void SomeMethod()
{
    while(true)
    {
        //do something
        if(condition)
            break;
    }
}

//somewhere else...
Thread t = new Thread(SomeMethod);
t.Start();

If so, then yes, it's a safe way to end the thread.

Upvotes: 3

Robert Harvey
Robert Harvey

Reputation: 180788

If you are referring to the Break() method in Parallel.For, it is specifically designed to request a safe abort of the looping iterations.

An example of this technique is described here.

Upvotes: 3

Brent Arias
Brent Arias

Reputation: 30165

A thread abort, which I think is what you meant, is not a proper technique to use - unless the thread is also separated by an appdomain barrier.

Upvotes: 3

Related Questions