Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 中断本机线程_C#_.net_Multithreading_Pinvoke_Native - Fatal编程技术网

C# 中断本机线程

C# 中断本机线程,c#,.net,multithreading,pinvoke,native,C#,.net,Multithreading,Pinvoke,Native,我目前正在研究Thread.Interrupt如何与p/Invoke或本机调用一起使用。我在MSDN中读到,不可能中止(Thread.abort)本机调用中的线程(其他用例也可能适用)。但是我没有找到任何引用,对于处于WaitSleepJoin状态的本机线程,它们的状态是相同的 这个问题不是关于是否应该调用Abort或Interrupt,而是关于在哪里可以找到关于这个的authoritive doc。为此,G-ing没有提供任何有用的输出 我的测试示例: #ifdef NATIVEFUNCTIO

我目前正在研究Thread.Interrupt如何与p/Invoke或本机调用一起使用。我在MSDN中读到,不可能中止(Thread.abort)本机调用中的线程(其他用例也可能适用)。但是我没有找到任何引用,对于处于WaitSleepJoin状态的本机线程,它们的状态是相同的

这个问题不是关于是否应该调用Abort或Interrupt,而是关于在哪里可以找到关于这个的authoritive doc。为此,G-ing没有提供任何有用的输出

我的测试示例:

#ifdef NATIVEFUNCTIONS_EXPORTS
#define NATIVEFUNCTIONS_API __declspec(dllexport)
#else
#define NATIVEFUNCTIONS_API __declspec(dllimport)
#endif

#include <iostream>

extern "C"
{
  NATIVEFUNCTIONS_API void EndlessWait(char const* mutexName)
  {
    std::cout << "entering the endless wait." << std::endl;

    HANDLE mutex = CreateMutex(NULL, FALSE, mutexName);
    WaitForSingleObject(mutex, INFINITE);

    std::cout << "leaving the endless wait." << std::endl;
  }

};
执行此应用程序将生成以下输出:

entering the endless wait.
Unable to terminate native thread.
Unable to interrupt the native wait.
Release the mutex.
leaving the endless wait.
Abort在这种情况下并不像预期的那样工作,但msdn并没有说一句关于中断的话。我希望它一方面可以工作:因为处于等待状态的托管线程也调用本机WaitForSingleObject或WaitForMultipleObject;另一方面,可能要中断的本机线程不支持expect exceptions all,除了什么

欢迎任何医生

非常感谢,
奥文斯


另外,我还发现,如果线程处于WaitSleepJoin状态,则中止将等待待中止的线程从非托管代码返回,并首先调用中断,然后中止。但这并不意味着中断不能中断本机WaitSleepJoin。

我怀疑线程是否处于WaitSleepJoin状态;中断被记录为仅在此状态下中断线程。查看线程的ThreadState属性以验证它处于何种状态。

实际上,线程处于运行状态。似乎.NET有自己的线程状态管理。在调用t.Abort()的行之后,线程处于AbortRequested状态,但thread.Interrupt不会中断它,因为它从未处于WaitSleepJoin状态。非常感谢!
entering the endless wait.
Unable to terminate native thread.
Unable to interrupt the native wait.
Release the mutex.
leaving the endless wait.