创建线程检查按钮的状态

DWORD WINAPI ThreadFunction( LPVOID lpParam )
{
    (void)lpParam;    //make happy compiler for unused variable

    while (TRUE)     //Once created the thread runs always
    {
        //If checked reads usb for each iteration
        if(SendDlgItemMessage(hWnd,START_BUTTON,BM_GETCHECK ,0,0)== BST_CHECKED)
        {
            char* var = USB_Read();   //Get data from the sensor
            SetWindowText(hLux, var); //Display the data
            Sleep(1);    //Why this? to don't have a furious CPU usage
        }
    }
}


.....

//Winmain
    DWORD dwThreadId;    //thread ID in case you'll need it
    //Create and start the thread
    CreateThread( 
                    NULL,           // default security attributes
                    0,              // use default stack size  
                    ThreadFunction, // thread function name
                    NULL,           // argument to thread function 
                    0,              // use default creation flags 
                    &dwThreadId);   // returns the thread identifier

......

case WM_COMMAND:
    switch (wp)
    {  
    case START_BUTTON:
        printf("START_BUTTON");
        if(SendDlgItemMessage(hWnd,START_BUTTON,BM_GETCHECK ,0,0)== BST_CHECKED)
            SendDlgItemMessage(hWnd,START_BUTTON,BM_SETCHECK ,BST_UNCHECKED, 0);
        else
            SendDlgItemMessage(hWnd,START_BUTTON,BM_SETCHECK ,BST_CHECKED, 0);
        break;
    }
    break;

猜你喜欢

转载自www.cnblogs.com/strive-sun/p/12468037.html