等待使用 SendMessage API 发送到 MainWindow 的消息

Wait for message delivered to MainWindow sent using SendMessage API

应用有多个进程,进程通过IPC通信。主进程是用 C# 编写的,具有 Windows 形式和 DefWndProc。另一个进程通过 SendMessage API 将消息发送到主进程 window,但是在 DefWndProc 中没有立即收到消息。有什么方法可以让我在接收过程中等待消息?

我已经在主程序中尝试了睡眠和定时器 window,但是只有在延迟后才能收到消息

Process A- WinForm Application,defwndproc 在这里实现 Process B-Use SendMessage API 发送消息给Process A Window

我的主要目的是立即处理发送到 main window 的消息,我可以看到消息没有立即传送到 DefWndProc

这是 C++ 中的示例代码,(删除了错误检查)

MainWindow.cpp:

#include <windows.h>
#include <iostream>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
DWORD WINAPI createyourwindow(LPVOID param)
{
    WNDCLASSEXW wcex = { 0 };

    wcex.cbSize = sizeof(WNDCLASSEX);
    HWND* hWnd = (HWND*)param;
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = GetModuleHandle(NULL);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszClassName = L"MyClass";
    RegisterClassExW(&wcex);
    *hWnd = CreateWindowW(L"MyClass", L"window", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, GetModuleHandle(NULL), NULL);

    if (!*hWnd)
    {
        return FALSE;
    }

    ShowWindow(*hWnd, SW_NORMAL);
    UpdateWindow(*hWnd);
    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, NULL, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return 0;
}

int main()
{
    DWORD ThreadId;
    HWND hwnd = NULL;
    HANDLE hThread = CreateThread(NULL, 0, createyourwindow, (LPVOID)&hwnd, 0, &ThreadId);
    MSG msg;
    DWORD tid = GetCurrentThreadId(); // used in the PostThreadMessage().
    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (msg.message == WM_USER && hwnd != NULL)
        {
            SuspendThread(hThread);
            printf("suspend\n");

            getchar(); //To Do here. 

            ResumeThread(hThread);
        }
    }
    WaitForSingleObject(hThread, INFINITE);
    return 0;

}

PostThreadMessage.cpp:

#include <windows.h>
#include <iostream>
int main()
{
    DWORD tid = 0x....;
    PostThreadMessage(tid,WM_USER,0,0);
    return 0;
}

在MainWindow.cpp中,在一个线程中创建主window,然后在主线程中还有一个消息循环,只是为了接收你的用户消息。然后挂起mainwindow的线程,做点自己想做的事,最后再恢复。

更新:

WinForm Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace WindowsFormsApp2
{
    static class Program
    {
        static uint WM_USER = 0x0400;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]

        static void Main()
        {
            Thread thread = new Thread(MainWindow);
            thread.Start();
            MSG msg = new MSG();
            uint tid = GetCurrentThreadId();
            while (GetMessage(ref msg, IntPtr.Zero, 0, 0) == 1)
            {
                if (msg.message == WM_USER)
                {
                    //Thread.Sleep(5000);  //You could still move the window for 5 second(just for test)
                    thread.Suspend();

                    Thread.Sleep(5000);  //To Do here. 

                    thread.Resume();
                }
            }
        }
        static void MainWindow()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        [DllImport("user32.dll")]
        private static extern int GetMessage(ref MSG msg, IntPtr hwnd, uint wMsgFilterMin, uint wMsgFilterMax);
        [DllImport("Kernel32.dll")]
        private static extern uint GetCurrentThreadId();
        private struct MSG
        {
            public IntPtr hwnd;
            public uint message;
            public uint wParam;
            public long lParam;
            public ulong time;
            public long x;
            public long y;
        }
    }
}

然后将消息发送到 tid