gpt4 book ai didi

c# - 无法终止 Silverlight 中的工作线程

转载 作者:太空狗 更新时间:2023-10-30 00:22:37 24 4
gpt4 key购买 nike

我正在开发一个多线程 Silverlight 应用程序。

该应用程序有两个线程:Main/UI 和一个后台工作线程。

UI 线程应该能够终止后台线程,如下所示:

private Thread executionThread;

....

executionThread = new Thread(ExecuteStart);
executionThread.Start();

....

executionThread.Abort(); // when the user clicks "Stop"

最后一行引发异常:

MethodAccessException: Attempt to access the method failed: System.Threading.Thread.Abort()

有什么想法吗?为什么我不能在 Silverlight 中中止线程?

谢谢,奈米

最佳答案

与其手动创建线程,不如考虑使用 BackgroundWorker类。

此类具有在 WorkerSupportsCancellation = true 时取消异步操作的内置功能。<​​/p>

请查看 MSDN 上的这篇文章 full example了解如何在 Silverlight 中使用 BackgroundWorker。

关于c# - 无法终止 Silverlight 中的工作线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/472431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com