windows - 调用Thread.Abort时finally不执行

标签 windows multithreading mono abort threadabortexception

有谁知道调用Thread.Abort时finally语句不被执行的原因吗?我有这样的线程函数:

void ThreadFunc()
{
    try
    {
        int counter = 0;
        while (true)
        {
            Debug.Log ("Waiting" + counter);
            Thread.Sleep(100);
            ++counter;
        }
        Debug.Log("Success.");
    }
    catch(ThreadAbortException ex)
    {
        Debug.LogWarning("Cancelled: " + ex);
    }
    catch(System.Exception ex)
    {
        Debug.LogError("Error: " + ex);
    }
    finally
    {
        Debug.Log("Finally");
    }
}

我使用 Thread.Abort 终止该线程。我在输出中看到的只是一堆等待,但没有其他行。我的主线程在调用 Thread.Abort 后继续运行(即子线程有足够的时间完成)。 Debug.Log 函数是线程安全的。

这种情况发生在 Windows 上。我正在使用 Mono(Unity 游戏引擎)。在 Mac 上它运行良好。在 iOS 上我得到了这种行为,但它似乎取决于设备/iOS 版本或 XCode 版本(我还没弄清楚)。在 Windows 上,这种情况每次都会发生。

顺便说一句:在 iOS 上,如果我将另一个try {} catch {}放入finally语句中,它就会崩溃。

有人知道为什么会发生这种情况吗?似乎与Thread.Abort和ThreadAbortException的文档所说的有很大不同。

最佳答案

Mono 不支持Constrained Execution Regions并且在调用Thread.Abort时不完全支持catch/finally。

有一个bug关于这个。

关于windows - 调用Thread.Abort时finally不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050342/

相关文章:

c - 线程创建和删除的开销与未使用线程的开销

c++ - 圈子用户头像问题

使用 Mono 的 WCF 证书安全性

.net - 如何从源代码安装 MonoDevelop 2.4?

linux - 如何在 Linux 上构建 Mono 应用程序

php - Mamp 无法启动 apache (Windows 10)

c++ - 如何将 IAudioClient3 (WASAPI) 与实时工作队列 API 一起使用

c - (高效)在 C 中将相同的数据写入多个文件

.net - 您能确保所有线程都以特定文化启动吗?

c++ - 终止正在运行的 boost 线程