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

标签 c# .net silverlight multithreading

我正在开发一个多线程 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/

相关文章:

javascript - 从 asp :CheckBox 获取文本

c# - 如何从 JObject 中按键获取值?

c# - 如何知道枚举是否有所有标志

c# - WinForms DataGridView - 数据绑定(bind)到具有列表属性(列数可变)的对象

silverlight - MVVM 绑定(bind)组合框

c# - 您可以/应该在 c# switch 语句中抛出异常吗?

c# - 如何在诺基亚 map for WP8 中设置 Tile Sources 的不透明度?

.net - 什么是红/绿测试?

.net - Silverlight 本地存储

silverlight - 在加载 Windows Phone 7 应用程序时将焦点设置到文本框