在 Windows Phone 7 中停止线程 [英] Stopping Thread in Windows Phone 7

查看:58
本文介绍了在 Windows Phone 7 中停止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在 WP7 中停止/中止/中断线程?

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

更新:想出了以下解决方案(队列事件是消费者/生产者 Q 的一部分,退出线程实际上不需要:)):

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;

        ....
    }
}

感谢马特!

非常感谢!

推荐答案

没有办法停止特定线程.如果你真的需要停止线程中运行的某些东西,你需要与线程中运行的代码进行通信并告诉它停止做它正在做的任何事情.没有内置的方法可以做到这一点.

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.

或者考虑使用 BackgroundWorker 因为这支持请求取消.

Alternatively consider a BackgroundWorker as this supports requesting cancellation.

这篇关于在 Windows Phone 7 中停止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆