1

I have a Windows app written in C (using gcc/MinGW) that works pretty well except for a few UI problems. One, I simply cannot get the progress bar to update from a thread. In fact, I probably can't get ANY UI stuff to update.

Basically, I have a spawned thread that does some processing, and from that thread I attempt to update the progress bar in the main thread. I tried this by using PostMessage() to the main hwnd, but no luck even though I can do other things like open message boxes. However, it's unclear whether the message box is getting called within the thread or on the main thread.

Here's some code:

// in header/globally accessible
HWND wnd; // main application window
HWND progress_bar; //progress bar

typedef struct { //to pass to thread
    DWORD mainThreadId;
    HWND mainHwnd;
    char *filename;
} THREADSTUFF;


//callback function
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
    switch (msg){
        case WM_CREATE:{
            // create progress bar
            progress_bar = CreateWindowEx(
                0,
                PROGRESS_CLASS,
                (LPCTSTR) NULL,
                WS_CHILD | WS_VISIBLE,
                79,164,455,15,
                hwnd,
                (HMENU)20,
                NULL,
                NULL);

            SendMessage(progress_bar, PBM_SETSTEP, 1, 0 );
            SendMessage(progress_bar, PBM_SETPOS, 0, 0 );
            //test to make sure it actually works
            SendMessage(progress_bar, PBM_STEPIT, 0, 0 ); //works fine
            SendMessage(progress_bar, PBM_STEPIT, 0, 0 ); //works fine
            SendMessage(progress_bar, PBM_STEPIT, 0, 0 ); //works fine
            SendMessage(progress_bar, PBM_STEPIT, 0, 0 ); //works fine

            break;
        }

        case WM_COMMAND: {
            if(LOWORD(wParam)==2){ //do some processing in a thread

                //struct of stuff I need to pass to thread
                THREADSTUFF *threadStuff;
                threadStuff = (THREADSTUFF*)malloc(sizeof(*threadStuff));
                threadStuff->mainThreadId = GetCurrentThreadId();
                threadStuff->mainHwnd = hwnd;
                threadStuff->filename = (void*)&filename;
                hThread1 = CreateThread(NULL,0,convertFile (LPVOID)threadStuff,0,NULL);

            }else if(LOWORD(wParam)==5){ //update progress bar

                MessageBox(hwnd,"I got a message!", "Message",  MB_OK | MB_ICONINFORMATION);
                PostMessage(progress_bar,PBM_STEPIT,0,0);
            }
            break;
        }
    }
}

This all seems to work okay. The problem is in the thread:

DWORD WINAPI convertFile(LPVOID params){

    //get passed params, this works perfectly fine
    THREADSTUFF *tData = (THREADSTUFF*)params;

    MessageBox(tData->mainHwnd,tData->filename,"File name",MB_OK | MB_ICONINFORMATION); //yep

    PostMessage(tData->mainHwnd,WM_COMMAND,5,0); //only shows message
    PostThreadMessage(tData->mainThreadId,WM_COMMAND,5,0); //does nothing
}

When I say, "only shows message," that means the MessageBox() function in the callback works, but not the PostMessage() to update the position of the progress bar.

If I use PostThreadMessage() to send a message to the main thread's message loop, I can intercept it and launch MessageBoxes so it's definitely working. However, even if I try to update the progress bar this way. it still won't update.

What am I missing?

5
  • This looks like the window procedure for your main window, right? There are no modal dialogs on the scene? Assuming that's the case, then here's a straw to clutch at: MSDN says that the PBM_STEPIT message arguments must both be zero, but you're passing CLR_DEFAULT which is 0xff000000. I also assume you haven't set the range to be 0-0 or something crazy like that. Dec 21, 2010 at 7:07
  • I noticed that, but it doesn't make a difference. Either value will work when it's called from the main thread, but neither will work when called form the worker thread. I updated the code to show 0,0.
    – Synthetix
    Dec 21, 2010 at 7:15
  • I figured it out. The problem was I had another HWND inside the thread that it created for itself, but the WNDCLASSEX variable name was the same name as the one in the main thread. I changed the variable name to something else and now it works. I don't know why this is, however. Even though the var is the same name, it should not effect the thread because it's not in the same scope, right?
    – Synthetix
    Dec 21, 2010 at 21:52
  • Sorry, I meant "class name," not variable name. The class you register with RegisterClassEx(). If that was named the same as the main thread's window's class, then the progress bar wouldn't update.
    – Synthetix
    Dec 21, 2010 at 23:06
  • Do you mean that you used the same name (PROGRESS_CLASS) in two calls to RegisterClassEx? The second such call would fail. Variable scoping isn't relevant here - internally, Windows keeps a list of registered classes. Then when you create your progress bar window it would have the same class as your main window, and that means it would use the same window procedure. That window procedure has no handler for WM_COMMAND/PBM_STEPIT. Dec 23, 2010 at 22:49

2 Answers 2

1

From the MSDN documentation for PBM_STEPIT:

wParam
    Must be zero.
lParam
    Must be zero.

CLR_DEFAULT is defined as 0xFF000000L. What happens if you change your code to:

PostMessage(progress_bar, PBM_STEPIT, 0, 0);
5
  • I tried that. It doesn't seem to make a difference whether 0 or CLR_DEFAULT is used.
    – Synthetix
    Dec 21, 2010 at 7:06
  • Have you confirmed that progress_bar is not NULL (i.e., CreateWindowEx() succeeded)? What happens if you use SendMessage() instead of PostMessage() to deliver PBM_STEPIT, and then force an update via UpdateWindow() right after that? EDIT: Ah, nevermind. I missed the part where you said the same code works fine when called from the main thread.
    – cbranch
    Dec 21, 2010 at 7:15
  • progress_bar is valid. In fact, just as a test I call PBM_STEPIT a few times right after creating it to verify that it's working, and it is (I see the bar fill up a little). Everything seems to work fine if it takes place on the main thread. Once I try to do anything in the worker thread (including sending messages to the main thread to update), nothing happens.
    – Synthetix
    Dec 21, 2010 at 7:23
  • Maybe use Spy++ to watch message traffic that is sent to the progress control and compare what happens when calling from the main thread vs. separate thread to see if you can spot any differences (e.g., is PBM_STEPIT actually delivered, and do you see a WM_PAINT happen shortly after that?)
    – cbranch
    Dec 21, 2010 at 14:33
  • A couple other suggestions: (1) get rid of PostThreadMessage() -- that won't work in your case because the posted message is not associated with any window, so won't be dispatched to your WndProc. (2) Check the return values from both calls to PostMessage(), and if there is a failure call GetLastError() to determine why it failed.
    – cbranch
    Dec 21, 2010 at 14:56
1

I suspect the problem lies in your message loop. Anyway then, three things:

  • I don't think theres any reason to post a PBM_STEPIT message, just send it.
  • Check your message loop and make sure you arn't doing something silly like GetMessage(&msg,hwnd,... - always pass NULL for the hwnd parameter for GetMessage otherwise posted messages destined for other windows will never get dispatched.
  • WindowProc's are always called by windows in the correct thread. So you can SendMessage, or PostMessage, directly from the worker thread to the progress control to update it.

bonus 4th point:

  • MessageBox creates a window, and runs its message loop in the calling thread. In your case all the windows are created and are running in the main thread. But there is no real minus to displaying message boxes in your worker thread (other than the fact that the worker threads processing will be stopped until the Message Box is closed).

Your Answer

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

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