SuspendThread and ResumeThread

记录下,用于复现

#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;

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

 

posted @ 2020-03-13 10:15  strive-sun  阅读(598)  评论(0编辑  收藏  举报