0

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

1
  • Check the return value of SendDlgItemMessage() to make sure it is returning >= 0 indicating success, and not LB_ERR or LB_ERRSPACE indicating failure. May 28, 2013 at 21:24

1 Answer 1

1

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.

3
  • 1
    In general, this is good advice from a design perspective. However, it is also valid and safe to send messages to UI controls in the main thread from worker threads using SendMessage() and related functions, since the messages will not be dispatched to the controls until the main thread pumps its message queue for new messages. This is clearly outlined in the SendMessage() documentation. May 28, 2013 at 21:22
  • @RemyLebeau: In theory it should work, yes. At the moment it doesn't (thus the question). Since he needs to do something to make it work anyway, why not do it right? It's not as if it's really a huge amount of extra work. May 28, 2013 at 21:26
  • Hi, and thanks for the responses, SendDlgItemMessage() returns 0 so that is fine, but still nothing within the listbox appears. I am a bit confused by what Jerry is saying about not manipulating UI elements directly (i am used to doing all my work in the main window procedure), can anyone provide a quick example of how to show this in code please. Thanks
    – David
    May 29, 2013 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.