测试事件

// 111111.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include 
#include "tchar.h"
#include "resource.h"
#include "tlhelp32.h"
#include 
#include 
#include 
#pragma comment(lib,"comctl32.lib")

HWND g_hwndDlg = NULL;

HANDLE g_hSet = NULL;
HANDLE g_hClean = NULL;

HANDLE g_Event = NULL;

HANDLE hThread[2] = {0};

int CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);

DWORD WINAPI t1(LPVOID lpargv)
{
    WaitForSingleObject(g_hSet,INFINITE);
    MessageBox(0,TEXT("自动设置1"),0,0);
    SetEvent(g_hClean);
    return 0;
}

DWORD WINAPI t2(LPVOID lpargv)
{
    WaitForSingleObject(g_hClean,INFINITE); 
    MessageBox(0,TEXT("自动设置2"),0,0);
    SetEvent(g_hSet);
    return 0;
}

DWORD WINAPI t3(LPVOID lpargv)
{
    WaitForSingleObject(g_hSet,INFINITE);   
    MessageBox(0,TEXT("自动设置3"),0,0);
    SetEvent(g_hClean);
    return 0;
}




DWORD WINAPI t11(LPVOID lpargv)
{
    WaitForSingleObject(g_Event,INFINITE);
    MessageBox(0,TEXT("手动设置1"),0,0);
    return 0;
}

DWORD WINAPI t22(LPVOID lpargv)
{
    WaitForSingleObject(g_Event,INFINITE);  
    MessageBox(0,TEXT("手动设置2"),0,0);
    return 0;
}

DWORD WINAPI t33(LPVOID lpargv)
{
    WaitForSingleObject(g_Event,INFINITE);  
    MessageBox(0,TEXT("手动设置3"),0,0);
    return 0;
}


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    InitCommonControls();//注册并初始化通用控件窗口类
    DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAIN_DLG),NULL,DialogProc);
    return 0;
}

int CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    //  g_hwndDlg = hwndDlg;
    
    switch(uMsg)
    {
    case WM_INITDIALOG://初始化dialog
        {
            
            return TRUE;        
        }
    case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
            case IDC_HAND_BTN:
                { 
                    
                    //第二个参数true通知类型,false互斥类型
                    g_Event = CreateEvent(NULL,TRUE,FALSE,NULL);
                    
                    hThread[0] = CreateThread(NULL,0,t11,hwndDlg,0,NULL);
                    hThread[1] = CreateThread(NULL,0,t22,NULL,0,NULL);
                    hThread[2] = CreateThread(NULL,0,t33,NULL,0,NULL);
                    
                    SetEvent(g_Event);
                    
                    WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
                    
                    CloseHandle(hThread[0]);
                    CloseHandle(hThread[1]);
                    CloseHandle(hThread[2]);
                    
                    CloseHandle(g_Event);
                    
                    return TRUE;
                }
            case IDC_AUTO_BTN:
                {
                    //第二个参数true通知类型,false互斥类型
                    g_hSet = CreateEvent(NULL,FALSE,TRUE,NULL);
                    g_hClean = CreateEvent(NULL,FALSE,FALSE,NULL);
                    
                    hThread[0] = CreateThread(NULL,0,t1,hwndDlg,0,NULL);
                    hThread[1] = CreateThread(NULL,0,t2,NULL,0,NULL);
                    hThread[2] = CreateThread(NULL,0,t3,NULL,0,NULL);
                    
                    WaitForMultipleObjects(3,hThread,TRUE,INFINITE);
                    
                    CloseHandle(hThread[0]);
                    CloseHandle(hThread[1]);
                    CloseHandle(hThread[2]);
                    
                    CloseHandle(g_hSet);
                    CloseHandle(g_hClean);
                    
                    return TRUE;
                }
            }
            
            
            return FALSE;
        }
    case WM_CLOSE:
        {
            EndDialog(hwndDlg,0);
            return TRUE;
        }
    }
    return FALSE;
}


你可能感兴趣的:(测试事件)