views:

242

answers:

2

I have a simple MFC program which displays the progressbar..I used the below code to display the progress bar..

HWND dialogHandle = CreateWindowEx(0,WC_DIALOG,L"Proccessing...",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                    600,300,280,120,NULL,NULL,NULL,NULL);
HWND progressBarHandle =  CreateWindowEx(NULL,PROGRESS_CLASS,NULL,WS_CHILD|WS_VISIBLE|PBS_MARQUEE,40,20,200,20,
            dialogHandle,(HMENU)IDD_PROGRESS,NULL,NULL);

while(FALSE == testResult)
    {
        MSG msg;
         SendMessage(progressBarHandle, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
        SendMessage(progressBarHandle,PBM_SETPOS,0,0);
        ShowWindow(progressBarHandle,SW_SHOW);
        Sleep(50);
             if(TRUE == myCondition)//myCondition is a bool variable which is decalred globally
            {

                DestroyWindow(dialogHandle);
                 AfxMessageBox(L"Test Success");
             }
        }

when I execute the above code..the message box displays only after a mouseover event.like if I move the mouse the message box will display if not it will not display until i move the mouse. And also while the progressbar is running if I try to move the progress bar window..it displays a windows background at the place of displacement and also in the new region or sometimes its getting stuck.Please help me with this!

EDIT2: message pumping.

while(PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE) && (FALSE == testResult))
    {
       if(msg.message == WM_QUIT)
      {
        DestroyWindow(dialogHandle);
        return TRUE;
       }
      SendMessage(progressBarHandle, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
      SendMessage(progressBarHandle,PBM_SETPOS,0,0);
       ShowWindow(progressBarHandle,SW_SHOW);
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        //return 1;
    }
A: 

I'm not sure what behavior you're trying to achieve.
The major problem is that your main window must process the messages in order to behave properly, and you aren't doing it.

while (FALSE == testResult) {
    MSG msg;
    // Here you `reset` the progress bar, one each turn, why?
    SendMessage(progressBarHandle, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
    SendMessage(progressBarHandle,PBM_SETPOS,0,0);
    ShowWindow(progressBarHandle,SW_SHOW);
    Sleep(50); // <-- here you PAUSE main thread for 50 milliseconds

    // myCondition is a bool variable which is decalred globally
    if (TRUE == myCondition) {
          DestroyWindow(dialogHandle);
          AfxMessageBox(L"Test Success");
     }

    // Here you'll loop, and you don't give the main thread a chance to
    // process the message queue.
}

In my opinion, instead of using a Sleep(50); you could setup a timer and use its callback to update the progress bar. That's a naive solution but you can give it a try.

Edit: Perhaps this? I didn't test it.

while (FALSE == testResult) {
    MSG msg;
    // Here you `reset` the progress bar, one each turn, why?
    SendMessage(progressBarHandle, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
    SendMessage(progressBarHandle,PBM_SETPOS,0,0);
    ShowWindow(progressBarHandle,SW_SHOW);
    Sleep(50); // <-- here you PAUSE main thread for 50 milliseconds

    // myCondition is a bool variable which is decalred globally
    if (TRUE == myCondition) {
        AfxMessageBox(L"Test Success")
        DestroyWindow(dialogHandle);
        return TRUE;
    }

    // Process the message queue
    while(PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE)) {
       if(msg.message == WM_QUIT) {
           DestroyWindow(dialogHandle);
           return TRUE;
       }
       TranslateMessage(&msg);
       DispatchMessage(&msg);
    }
}
Nick D
okay,can you tell me how it can be handled by main thread.
kiddo
@kiddo, don't you prefer to use a timer?
Nick D
yes Nick,I can do that with timer..but this thing I just wanted to know the cause of the problem and I how it can be handles(just for knowledge).is it the same cause(that u xplained) that makes the progress bar window stuck when its been moved?
kiddo
@kiddo, MFC framework hides an internal message pump, see Get/Dispatch/TranslateMessage loop http://msdn.microsoft.com/en-us/library/ms644936%28v=VS.85%29.aspx You can embed in your code such a loop if you want. And yes, that can also cause the window to stuck.
Nick D
check my edit..i have the same problem again
kiddo
@kiddo, check out my edit.
Nick D
A: 

AfxMessageBox calls the MessageBox on the CWinApp (Obtainable through CWinApp). As such I'm going to guess that the code you posted is in a thread (also due to your recent posts). Therefore when you call the AfxMessageBox it is waiting on the main thread for it actually to get displayed.

If you call

 MessageBox( NULL, L"Test Success", NULL, MB_OK );

does it work then? If so this is very likely to be your issue...

Edit: Hmm tis a hard one because I transplanted your original loop into an empty MFC app and it worked fine. The problem must exist in some other piece of code somewhere ... What does the thread that sets myCondition look like?

Goz
Goz,actually this function is not in the thread..but there is a another function which is running byside(which is independent of UI).I also tried MessageBox..its still the same...and for your information..I tried pumping the message..its still the same
kiddo
please check my edit
kiddo