|
-
March 26th, 2012, 01:06 PM
#1
Witing without blocking the code
Hello
I have a function that does a wait on a thread but allows call backs to get in without exiting the thread.
so all i want is a way to avoid thread from exiting but still allowing the callbacks to be processed.
void MsgLoop(){
MSG message;
while (GetMessage(&message,NULL,0,0) && !StopFlag) {
OutputDebugString("MSGLOOP");
TranslateMessage( &message );
DispatchMessage( &message );
}
}
this works perfectly but i dont know how to exit that loop voluntary i tried adding a flag to the while as you can see... but it doesnt ever return from the GetMessageCall.
Sleep doesnt work cause it suspend the thread preventing the call backs to be processed.
any ida to solve this problem?
Thx In Advance!!!
-
March 26th, 2012, 01:15 PM
#2
Re: Witing without blocking the code
this works perfectly but i dont know how to exit that loop voluntary i tried adding a flag to the while as you can see...
You are using GetMessage instead of PeekMessage.
Code:
MSG stMsg;
while (::PeekMessage (&stMsg, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage (&stMsg);
::DispatchMessage (&stMsg);
}
-
March 26th, 2012, 01:15 PM
#3
Re: Witing without blocking the code
Just create a user defined message and a handler in the UI thread to handle the message.
Disable any UI controls as appropriate, fire off the worker thread, and before the worker thread exits, send or post the user defined message from the worker thread.
In the thread completed handler, reenable the UI controls.
-
March 26th, 2012, 02:46 PM
#4
Re: Witing without blocking the code
 Originally Posted by Skizmo
You are using GetMessage instead of PeekMessage.
Code:
MSG stMsg;
while (::PeekMessage (&stMsg, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage (&stMsg);
::DispatchMessage (&stMsg);
}
this will exit immediately and i need the thread to stay alive once this exits the thread dies and wont be able to process the callbacks.
getmessage does just that prefectly the only problem is that at somepoint i need to exit voluntary that message loop.
Just create a user defined message and a handler in the UI thread to handle the message.
Disable any UI controls as appropriate, fire off the worker thread, and before the worker thread exits, send or post the user defined message from the worker thread.
In the thread completed handler, reenable the UI controls.
i was trying to do just this yesterday but im not sure how to do this properly i have a thread in a console app.
i was thinking about "PostThreadMessage" since i have the thread ID but would this work and be a good practice as well?
or how would do that...
Edit:
would this function "WaitForSingleObject" wait in the same way GetMessage does it?
all i need is to keep the thread alive while i process callbacks.
i appreciate ur help!!
Last edited by Alphadan; March 26th, 2012 at 02:51 PM.
-
March 26th, 2012, 02:54 PM
#5
Re: Witing without blocking the code
 Originally Posted by Alphadan
i was trying to do just this yesterday but im not sure how to do this properly i have a thread in a console app.
i was thinking about "PostThreadMessage" since i have the thread ID but would this work and be a good practice as well?
or how would do that...
i appreciate ur help!!
Typically a console app doesn't deal with messages or message pumps, so you probably need to change your approach.
I assume that you have some sort of loop in the console app that processes user input. If so, when appropriate, fire off the worker thread and store the thread handle.
Then, use WaitForSingleObject( hWorker, 0 ) == WAIT_OBJECT_0 to check if the thread is still running. This statement will return true when the thread has exited.
-
March 26th, 2012, 04:44 PM
#6
Re: Witing without blocking the code
 Originally Posted by Arjay
Typically a console app doesn't deal with messages or message pumps, so you probably need to change your approach.
I assume that you have some sort of loop in the console app that processes user input. If so, when appropriate, fire off the worker thread and store the thread handle.
Then, use WaitForSingleObject( hWorker, 0 ) == WAIT_OBJECT_0 to check if the thread is still running. This statement will return true when the thread has exited.
while(!WaitForSingleObject(hThread, 0) == WAIT_OBJECT_0){
}
this works perfectly it allows me to exit once the thread has ended but... it blocks the code and doesnt allow the callbacks to be processed...
I need something that prevents the thread from exiting like this but NOT blocking the code... or voluntary exiting the MsgLoop()
void MsgLoop(){
MSG message;
while (GetMessage(&message,NULL,0,0) && !StopFlag) {
TranslateMessage( &message );
DispatchMessage( &message );
}
}
Any ideas D: Thx in advance.
-
March 26th, 2012, 05:20 PM
#7
Re: Witing without blocking the code
Again, console apps usually don't have message pumps. So if this is a console app, what do you want the app to do while waiting for the thread to finish? Is it in some loop waiting for the user to press a key?
-
March 26th, 2012, 06:31 PM
#8
Re: Witing without blocking the code
this works perfectly it allows me to exit once the thread has ended but... it blocks the code and doesnt allow the callbacks to be processed...
You expect some advice about how to overcome your architectural problems, but you never explained your architecture. Don't you find this a bit weird?
- What is the app type, console or window based?
- What the other threads' types are? Windowless or windowed?
- What is the callback you always mention?
- Could you come up with a compilable and concise code/project showing the problem you're trying to solve?
Last edited by Igor Vartanov; March 26th, 2012 at 06:34 PM.
Best regards,
Igor
-
March 26th, 2012, 09:27 PM
#9
Re: Witing without blocking the code
well the callback is a function called externally by its address, i dont want the thread to exit cause if it exits it wont process callbacks.
Sleep, WaitForSingleObject lock the thread and wont allow the incoming callbacks to be processed all i want to know is why the lock made by GetMessage does not block the code anyone knows why?
i think ill have to do more deep research myself on the net but thx for your time any ways.
-
March 27th, 2012, 02:16 AM
#10
Re: Witing without blocking the code
Solved... the thing was that there were no messages to process so GetMessage Never Returned...
so i deliverated sent a message with "PostThreadMessage" to that spesific thread to "Unlock" and exit voluntary.
Thx everyone for your time
-
March 27th, 2012, 07:11 AM
#11
Re: Witing without blocking the code
 Originally Posted by Alphadan
Thx everyone for your time
Your welcome, although it might be more helpful the next time if you would answer the questions asked by the folks trying to help you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|