Reputation: 35
My status bar does not refresh during the program execution and I don't know why. First the pseudo-code. I have deleted most of it, leaving only the idea.
#include ... (many includes)
using namespace std;
#include "MyHeaderFile.hpp"
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
//here some global variables. Thereare more of them, I leave only teh important ones.
HWND g_hButtonStart;
MSG msg;
//----------------------------------- windows ------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ WNDCLASSEX wc;
HWND hwnd;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(240,240,240));//(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//window class register
if(!RegisterClassEx(&wc))
{ MessageBox(NULL, "Rejestracja klasy okna nie powiodła się!","BMP->DXF: Błąd!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_WINDOWEDGE,"WindowClass","BMP -> DXF",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
275,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL)
{ MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
//group boxes, text boxes, buttons ...
//most important are the "START" button and the status bar:
g_hButtonStart = CreateWindowEx( 0, "BUTTON", "S T R T", WS_CHILD | WS_VISIBLE, 243, 133, 100, 30, hwnd, NULL, hInstance, NULL );
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
//status bar things
INITCOMMONCONTROLSEX icmc;
icmc.dwSize = sizeof( INITCOMMONCONTROLSEX );
icmc.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx( & icmc );
g_hStatusBar = CreateWindowEx( 0, STATUSCLASSNAME, NULL, SBARS_SIZEGRIP | WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd,( HMENU ) 200, hInstance, NULL );
SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "some info" );
//status bar - end
while(GetMessage(&msg, NULL, 0, 0))
{ if (!IsDialogMessage(hwnd, &msg))
{TranslateMessage(&msg);
DispatchMessage(&msg); }
}
return msg.wParam;
}
//messages
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{ bool ParameterWarning=false;
string Info;
switch(Message)
{ case WM_CLOSE:
{ DestroyWindow( hwnd );
}
break;
case WM_DESTROY:
{ PostQuitMessage(0);
break;
}
//here is the most important part of the code
case WM_COMMAND:
{ if(( HWND ) lParam == g_hButtonStart )
{ //enabling and disabling some buttons, edit boxes etc.
//some variables declarations and initializations...
SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 1" );
//opening the in file ...
//reading file parameters and checking for errors ...
SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 2" );
//here some large calculations start, lasting for 30mins, for example; written in C++
for(int N=0;N<one_of_the_variables;N++)
{
//calculations part 1; also writing to out file ...
SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 3" );
//calculations part 2; also writing to out file ...
SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 4" );
//calculations part 3; also writing to out file ...
SendMessage( g_hStatusBar, SB_SETTEXT, 0,( LPARAM ) "message 5" );
}
//enabling and disabling some buttons, edit boxes etc. ...
//closing the out file ...
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
During the calculations, my main window displays "(Not Responding)" in the title. It doesn't bother me (yet). The status bar shows only some of the messages - it seems to be random. Sometimes only the last message, sometimes two or three of them. But it never shows them all correctly during the whole calculation. What should I do to make it work?
Upvotes: 2
Views: 1082
Reputation: 612964
The not responding is another symptom of the problem. A GUI app needs to process its message queue frequently. It is this act of pumping the message queue that allows the UI to update. Your long running tasks stops the message queue from being pumped and leads to the various problems that you report.
The solution is to pump the message queue frequently. Don't perform long running tasks in the main thread because that stops you being able to service the message queue. Move these tasks into a separate thread.
Upvotes: 1