用户定义的消息无效

时间:2013-06-09 08:12:14

标签: multithreading winapi

我想要完成的事情的简要总结 - 我'试图'编写代码,允许操作员使用winapi线程同时测试4个设备。测试单元的代码工作正常,线程工作。在一个线程中,我想将传递或失败消息发送到主窗口(hwnd)上的列表框。这是一些代码...

#define WM_TEST_RESULT (WM_USER+1)

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static INT i;
RECT rect;

switch (message)
{
// This never gets called while running
case WM_TEST_RESULT:
    i = lParam;
    if(test1[i].passed == true)
        SendDlgItemMessage(hwnd,IDT_RESULTLIST1,LB_ADDSTRING,i,(LPARAM)"PASS");
    else SendDlgItemMessage(hwnd,IDT_RESULTLIST1,LB_ADDSTRING,i,(LPARAM)"FAIL");
    MessageBox(hwnd,"test",0,0); //debug
break;
case WM_CREATE:
    hText14 = CreateWindowEx(WS_EX_CLIENTEDGE,WC_LISTBOX,0,WS_VISIBLE|WS_TABSTOP|WS_CHILD,
                165,185,40,325,hwnd,(HMENU)IDT_RESULTLIST1,hInst,FALSE);
break;
case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);

    switch(LOWORD(wParam))
    {
        case IDB_STARTBUTTON1:

        break;
        case IDB_STARTBUTTON2:

        break;
        case IDB_STARTBUTTON3:

        break;
        case IDB_STARTBUTTON4:

        break;
        case IDB_RUNTESTBUTTON1:
            _beginthread(Thread1,0,NULL);
            break;
        case IDB_RUNTESTBUTTON2:
            _beginthread(Thread2,0,NULL);
            break;
        case IDB_RUNTESTBUTTON3:
            _beginthread(Thread3,0,NULL);
            break;
        case IDB_RUNTESTBUTTON4:
            _beginthread(Thread4,0,NULL);
            break;
    }   
    // Parse the menu selections:
    switch (wmId)
    {
    //case IDM_ABOUT:
        //DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, About);
    //  break;
    case IDD_PROPPAGE_MEDIUM:
        DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_PROPPAGE_MEDIUM), hwnd, Config);
        break;
    case IDM_EXIT:
        DestroyWindow(hwnd);
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    break;
case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    // TODO: Add any drawing code here...
    EndPaint(hwnd, &ps);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}

这是调用线程......

void Thread1(PVOID pvoid)
{
for(int i=0;i<numberOfTests1;i++) {     
    int ret;
    double TimeOut = 60.0;
    int Lng = 1;
    test1[i].testNumber = getTestNumber(test1[i].testName);
    //test1[i].testNumber = CMD_TOOL_BUZZER; //debug only
    unsigned char Param[255] = {0};
    unsigned char Port1 = port1;

    ret = PSB30_Open(Port1, 16);
    ret = PSB30_SendOrder (Port1, test1[i].testNumber, &Param[0], &Lng, &TimeOut); 
    ret = PSB30_Close (Port1);
    int result = 0;

    if(*Param == 1) {
        test1[i].passed = true;
        SendMessage(hwnd,WM_TEST_RESULT,i,(LPARAM)"PASS");
    }else {
        test1[i].passed = false;
        SendMessage(hwnd,WM_TEST_RESULT,i,(LPARAM)"FAIL");
    }   
}
_endthread();
}

我查看了其他源代码,无法看到与我想要完成的任何内容有什么不同,有没有人有任何想法?如果需要,我可以添加更多的源代码,但我认为在这段代码中有一些是问题所在。

干杯

1 个答案:

答案 0 :(得分:0)

WM_USER消息是为窗口类作者保留的,最好将WM_APP + xxx用于应用程序私有消息。它的窗口是一个对话框,例如,WM_USER + 1不应该按预期工作。有关详细解释,请参阅here