C#相当于另一个进程的posttrheadmessage 我有C++编写的代码,它控制CealPrcess()和其他Windows API(BR>)创建的其他进程(“客户端进程”)。 然后,客户端进程(控制台、单线程)等待来自“服务器”进程的消息MSG_OK,并在收到消息后恢复运行。 我使用了PostThreadMessage(),效果很好 int MSG_OK = RegisterWindowMessage("MSG_OK"); void run(TCHAR* path) { STARTUPINFO si={0,}; si.cb=sizeof(STARTUPINFO); si.dwFlags=0; PROCESS_INFORMATION pi; CreateProcess(NULL, path, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); Sleep(1000); // Just for simplicity. Actual code is message-based PostThreadMessage(pi.dwThreadId, MSG_OK, 0, 0); CloseHandle(pi.hProcess); // closing process handle makes usage count 1 CloseHandle(pi.hThread); }

C#相当于另一个进程的posttrheadmessage 我有C++编写的代码,它控制CealPrcess()和其他Windows API(BR>)创建的其他进程(“客户端进程”)。 然后,客户端进程(控制台、单线程)等待来自“服务器”进程的消息MSG_OK,并在收到消息后恢复运行。 我使用了PostThreadMessage(),效果很好 int MSG_OK = RegisterWindowMessage("MSG_OK"); void run(TCHAR* path) { STARTUPINFO si={0,}; si.cb=sizeof(STARTUPINFO); si.dwFlags=0; PROCESS_INFORMATION pi; CreateProcess(NULL, path, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); Sleep(1000); // Just for simplicity. Actual code is message-based PostThreadMessage(pi.dwThreadId, MSG_OK, 0, 0); CloseHandle(pi.hProcess); // closing process handle makes usage count 1 CloseHandle(pi.hThread); },c#,c++,interop,pinvoke,ipc,C#,C++,Interop,Pinvoke,Ipc,现在我想用C#重写代码,尽可能少地使用p/invoke。 以下是我正在编写的代码: [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true)] public static extern bool PostThreadMessage(uint threadId, int msg, IntPtr wParam, IntPtr lParam); public void run(strin

现在我想用C#重写代码,尽可能少地使用p/invoke。 以下是我正在编写的代码:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostThreadMessage(uint threadId, int msg, IntPtr wParam, IntPtr lParam);

public void run(string path) {
    Process proc = new Process();
    ProcessStartInfo si = new ProcessStartInfo();
    si.FileName = path;
    proc.StartInfo = si;
    proc.Start();
    uint threadId; // <----?
    PostThreadMessage(threadId, MSG_OK, 0, 0);
    proc.Dispose();
}
似乎是我要找的threadId。我做错什么了吗?

请看


<>我会考虑使用一些其他的方法,比如.< /p>,也许会帮助TestRead Mebug()很麻烦。改用管道,在C#中也得到很好的支持,而不必使用pinvoke。通过使用PostThreadMessage(),我可以避免多线程和其他同步问题。在我看来,只要客户端进程是单线程的,并且没有处理用户输入的UI,那么使用PostThreadMessage实现服务器比使用管道更容易。考虑到对客户端进程的限制,使用PostThreadMessage还有其他缺点吗?@HansPassant管道是PostThreadMessage最差的替代方案。
 proc.Threads[0].id