Grigory
Grigory

Reputation: 1922

Stopping Thread in Windows Phone 7

How can i stop/abort/interrupt thread in WP7 ?

Update: Came up with the following solution (queue event is part of consumer/producer Q and is not actually required for exiting thread :)):

protected override void Dispose(bool disposing)
{
    base.Dispose(disposing);
    //GP-HACK:Stoping the thread.
    stopExecutorThread = true;
    operationQueueNonEmptyEvent.Set();
}

private volatile bool stopExecutorThread = false;

public void Run()
{
    do
    {
        operationQueueNonEmptyEvent.WaitOne();

        if (stopExecutorThread) 
            return;

        ....
    }
}

Thanks to Matt!

Thanks a lot!

Upvotes: 1

Views: 965

Answers (1)

Matt Lacey
Matt Lacey

Reputation: 65556

There isn't a way to stop a specific thread. If you really need to stop something runnign in a thread you'll need to communicate with the code running in the thread and tell it to stop doing whatever it's doing. There is no built in way to do this.

Alternatively consider a BackgroundWorker as this supports requesting cancellation.

Upvotes: 2

Related Questions