c# - 在 Azure 辅助角色中使用 kernel32 CreateThread/TerminateThread

标签 c# azure dllimport azure-worker-roles native-code

我有这个代码需要使用,但阻碍我的是 here它说 TerminateThread 适用于:仅桌面应用程序。 我想知道是否可以在 azure Worker 角色中使用此代码,特别是在我用来运行 Worker 线程的 waiishost.exe 进程中?

[DllImport("Library.dll")]
public static extern void InfiniteLoop();

[DllImport("kernel32")]
private static extern int CreateThread(
   IntPtr lpThreadAttributes,
   UInt32 dwStackSize,
   IntPtr lpStartAddress,
   IntPtr param,
   UInt32 dwCreationFlags,
   UInt32 lpThreadId
 );

[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern int TerminateThread(int hThread);

[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetLastError();

private delegate int InvokeInfiniteLoop(IntPtr args);

static void Main(string[] args)
{
      InvokeInfiniteLoop invokeInfiniteLoop = (args1) =>
                                                    {
                                                        InfiniteLoop();
                                                        return 0;
                                                    };
     IntPtr infiniteLoopPtr = Marshal.GetFunctionPointerForDelegate(invokeInfiniteLoop);
     int handle = CreateThread(IntPtr.Zero, 0, infiniteLoopPtr, IntPtr.Zero, 0, 0);
     Thread.Sleep(TimeSpan.FromSeconds(5));
     int terminated = TerminateThread(handle);
     Console.WriteLine(terminated);
}

编辑:

经过进一步的研究,看起来(正如我从一开始就怀疑的那样)这个解决方法是完全错误的。创建和终止非托管代码将泄漏堆栈。我将必须创建一个单独的可执行文件,该可执行文件将由 Process.Kill();

最佳答案

“仅适用于桌面应用程序”是指 Windows 8 桌面应用程序和 Windows 8 Metro 应用程序之间的区别。

关于c# - 在 Azure 辅助角色中使用 kernel32 CreateThread/TerminateThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12043408/

相关文章:

azure - 我可以使用 Id 之外的自定义键创建 azure cosmos db 文档吗

angular - 在我的 Angular2 环境中使用 typescript 使用/导入 azure-storage

c# - 用优雅来逃避进口大象 : a case of excessive dll Imports

visual-studio - : app does not see func from my dll如何处理

c# - 如何在 Windows Phone 8.1 应用程序中扫描二维码? (不是 silverlight 应用程序)?

c# - 统一。哪个更快 : one big script with switch(ObjectType) or lot of simple scripts?

azure - "The deserializer has no knowledge of any type that maps to this name..."上的 Service Fabric 错误

c# - 快速查找可枚举对象中的结果

c# - WinRT 应用程序 : ComboxBox and ListBox does not display anything when ItemsSource is List<Type>

c++ - 如何安全地从DLL调用返回对象