David
David

Reputation: 5

sending messages to the main window in a thread in win32

I am relatively new to working with threads in Win32 api and have reached a problem that i am unable to work out.

Heres my problem, i have 4 threads (they work as intended) that allow the operator to test 4 terminals. In each thread i am trying to send a message to the main windows form with either Pass or Fail, this is placed within a listbox. Below is one of the threads, the remaining are exactly the same.

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

    if(*Param == 1) SendDlgItemMessage(hWnd,IDT_RESULTLIST1,LB_ADDSTRING,i,(LPARAM)"PASS");
    else SendDlgItemMessage(hWnd,IDT_RESULTLIST1,LB_ADDSTRING,i,(LPARAM)"FAIL");
}
_endthread();
}

I have debugged the code and it does everything except populate the listbox, i assume because its a thread i am missing something as the same code works outwith the thread. Do i need to put the thread to sleep while it sends the message to the main window?

Any help is appreciated.

Cheers

Upvotes: 0

Views: 1647

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490663

You don't want your secondary threads trying to manipulate your UI elements directly (such as the SendDlgItemMessage). Instead, you normally want to post something like a WM_COMMAND or WM_USER+N to the main window, and let that manipulate the UI elements accordingly.

Upvotes: 1

Related Questions