0

我正在尝试完成的任务的简要总结 - 我正在“尝试”编写代码,允许操作员使用 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();
}

我查看了其他源代码,但看不到与我要完成的工作有什么不同,有人有什么想法吗?如果需要,我可以添加更多源代码,但我认为此代码中的某些问题是问题所在。

干杯

4

1 回答 1

0

WM_USER 消息是为窗口类作者保留的,应用程序私有消息最好使用 WM_APP + xxx。如果您的窗口是一个对话框,例如,WM_USER + 1 不应按预期工作。有关详细说明,请参见此处

于 2013-06-10T06:52:36.453 回答