Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, guys
If the message handling time exceeds 5 seconds, the Window goes to "Not responding" state and the messages in message queue is not processed. Is there a simple function call to handle messages so that the window does not freeze?
Thanks in advance
mr.abzadeh
Posted

1 solution

Typically, you need to do all lengthy operation in a different thread, other then UI thread. Manipulating with handling message is a really bad idea.

[EDIT: unrelated part of post removed; I did not notice this is a C application]

Any particular reason to use C, not even C++?

—SA
 
Share this answer
 
v2
Comments
mr.abzadeh 24-Jan-13 18:12pm    
Thanks, threading Ok, but for passing results to UI, I need some help in windows API because I program in native C.
Is calling the following function a good idea, somewhere in my lengthy calculation periodically?
//-------------------------------------------------------------
void uDoEvents( HWND hWnd )
{
MSG uMsg;
while ( PeekMessageW( &uMsg, hWnd, 0, 0, PM_NOREMOVE ) )
{
int bRet = GetMessageW( &uMsg, hWnd, 0, 0 );
if ( bRet == 0 ) break;
if ( bRet == -1 ) break;
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
}
Sergey Alexandrovich Kryukov 24-Jan-13 18:22pm    
Sorry, I did not notice this is a C application.
Threads, anyway. Don't get into trouble...
—SA
mr.abzadeh 24-Jan-13 18:24pm    
For example, I created a new thread to burn data to cd, calling ICDBurn::Burn( hWnd ), buf the ICDBurn::Burn method appeared under my window, meaning that I should not pass hWnd to ICDBurn::Burn, because the calling thread had no window. How I should tell ICDBurn::Burn that hWnd will be it's Parent Window?
Sergey Alexandrovich Kryukov 24-Jan-13 19:01pm    
There are two aspect of this: passing parameter and synchronization.

Google "pass parameter to thread windows", you will find out; thread method has a void pointer which you can use to pass any structure pointer.

Synchronization: you cannot do any UI operation in the non-UI thread; if you need to notify UI, pass HWND to thread and use PostMessage to pass some data to a window, with any information you want. You will pick up (handle) those messages in from a usual message processing mechanism. Better use some custom message ID, use anything started from WM_USER:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644931%28v=vs.85%29.aspx

This is a raw style of doing it all.

—SA
mr.abzadeh 26-Jan-13 1:08am    
Ok, It's good idea, but It may not help passing parameter hWnd in case lengthy calculations, at least in this case:
I have Window A, that wants to call this code:
ICDBurn::Burn( hWndA );
ICDBurn::Burn creates It's own Window, Say Window B, and sets Its parent window to hWndA, and Burns data to CD(lengthy calculation).
If I call ICDBurn::Burn( hWndA ) from a non-window thread, It works, But Window B will appear behind Window A, and my application's users will not notice It, and I can not post any message to hWndB because I do not have It's code. The only way is calling ICDBurn::Burn( hWndA ) from window A, or possibly another window, but this freezes the calling window and even controls on It. for example, the progress controls on Window A freeze. So, I think It's necessary to have a way to call UI functions that take long time, from a window thread, without freezing the calling window.
Can you please show me an Idea?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900