|
Create a mutex that the worker thread needs to go into when it requests the main thread to display the UI update. When the main thread is waiting on the worker thread it can acquire the mutex. Then when the worker thread is not able to aquire the mutex, it will simply skip the UI update process.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Like that idea, my only other solution is to use messages to request the UI update process. Just hoping that they are ignored when in the wait state.
Cheers
|
|
|
|
|
Obviously it is best to post messages from the worker thread to the UI thread instead of sending them. If the UI thread is blocked by something, the worker thread can continue its Job and you don't get deadlocks.
In multithreading it is always a good idea to make as few synchronsiation points in your programm execution as possible. Asynchronous message passing using PostMessage() is a good mechanism to achieve this.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
The best solution is to use the MsgWaitForMultipleObjects API.
Also, as someone pointed out, don't use SendMessage in your worker thread when you want to send a message to the main thread, use PostMessage.
It'll look something like this:
HANDLE hThread =
DWORD dwWait;
do{
dwWait = MsgWaitForMultipleObjects(1,
&hThread,
FALSE,
5000,
QS_ALLINPUT);
switch( dwWait )
{
case WAIT_OBJECT_0:
break;
case WAIT_OBJECT_0 + 1:
{
MSG msg;
while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}break;
case WAIT_TIMEOUT:
{
}break;
default:
{
}break;
}
}while( dwWait != WAIT_OBJECT_0 );
The above code snippet assumes that your worker thread exits when it's finished doing it's thing. If it hangs around in a wait state for future requests, it's best to create an event object which is signalled when the worker thread finishes it's task & then have the above code snippet wait on the event handle instead of the thread handle.
FYI, the thread handle becomes 'signalled' when the thread exits. Signalled means that any of the wait functions will return.
HTH,
Wes
Sonork ID 100.14017 wtheronjones
|
|
|
|
|
I found out that I am scheduled to teach an AP Computer Programming class in Visual C++ this fall.
I'm trilled to find this site that has been a great help already, but I need to order the program and the textbooks. Any suggestions and comments would be appreciated.
Sherry
|
|
|
|
|
Is there already a cirriculum defined for the class? If so what will be the main focus?
Learning to program C++, Learning to program inside of the Visual C++ environment, Learning to program windows programs?
If you give a little bit more detail I think it would be a little easier to recommend some books for you.
As far as Visual C++, you should be able to get a really good acedemic discount on the standard version for your students.
Good Luck
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
In a sample, I see that when the _RecordsetPtr objec tis created, its closed after we're done with it. Same for the Connection object.
What about the _CommandPtr object. After we get through, is there close() for it too?
I didnt see anything about it in MSDN or the web....
Thanks,
ns
|
|
|
|
|
Should i use a postmessage or a sendmessage in a thread to send information back to my dialog. I need the messages to get there in the order i sent them, does postmessage do this all the time?
thanks
Scott
|
|
|
|
|
Both will get the message to your window. SendMessage will block your thread, PostMessage won't.
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Ya the problem I am having is when exiting the app, i want to exit the thread. If i use send message and on exit I
WaitForSingleObject(hHandle, INFINTE)
then it freezes the main program and never lets the thread back to end.
I could use send message but I'm not sure if i want to or not.
Is there a better way of ending threads?
|
|
|
|
|
If I understand, your main thread is blocked waiting for the second thread to exit, but you also want the second thread to send a message to the dialog right before it exits? You should use MsgWaitForMultipleObjects() which will return if a message appears in the queue. ATL has a handy function AtlWaitWithMessageLoop() as well.
--Mike--
Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
What the thead does is do some file reads and some calculating and then sends the inforation back to the main program to be put in a list box.
So on exit of the app say MsgWaitForMultipleObjects() wich will allow the message that the thead sent to continue but wait the main program to exit until hHandle has been set that i'm waiting on?
Scott
|
|
|
|
|
MsgWaitForMultipleObjects() will unblock when either the handle is signaled, or a message is put in the queue. So you'd loop while the return value is WAIT_OBJECT_0+1. If that's returned, a message was posted, so call GetMessage/TranslateMessage/DispatchMessage to process it, then wait again. When the return value is WAIT_OBJECT_0, the handle is signaled (meaning the thread has exited).
--Mike--
Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
I use SendMessage() to update the main thread of new changes. However I use PostMessage() right before returning from the thread so the main thread can close the thread.
Kuphryn
|
|
|
|
|
Like most people here, I like small, compact and optimized code. Therefore I among other things, I ignore the default libraries when I can, and use a <font color = blue> void main()</font> instead of the default WinMain entrypoint.
I was implemeting ADO as described here[^] in an app, which has such a void main() entry point. After successfully instantiating a ADODB.Connection object, I cannot use any of its functions normally.
pConn->Open(...) causes an unknown exception,
pConn->raw_Open(...) works fine.
pConn->Execute(...) causes an unknown exception, pConn->raw_Execute(...) works fine.
then after changing the void main to the standard WinMain, everything pConn 'behaves' normal.
What is void main() lacking, what WinMain isn't?
Georg Haan, NL
|
|
|
|
|
You're using the #import wrappers, right? Those use _bstr_t which is a MS-provided class that definitely needs the CRT. Not linking with the CRT is causing your troubles.
--Mike--
Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Hi,
in on the recent threads, I've suggested to try doing a callback data supply to my listview control, which I think will be very useful. But I can't figure out how to force the listview to send me the LVN_GETDISPINFO message. I've set the text for each item to LPSTR_CALLBACK and don't have the ASCENDING/DESCENDING styles set. I know this is probably wrong as I figured out it only takes the data in some structure or what. But I need a function. How can I do it?
Thanks very very much
|
|
|
|
|
Hi,
I have a c++ app which posts an XML string to a web site where the data is parsed and inserted into a database. What I need to do is encrypt the XML string before it is sent and decrypt it on the server before it is parsed.
The algorithym needs to be simple so that I can implement it in VBScript on the server but not so simple that it is easy to decipher.
Can anyone make any suggestions?
Cheers
James
|
|
|
|
|
Try this[^]
cheers,
Chris Maunder
|
|
|
|
|
That's absolutely spot on Chris. Thanks loads
Cheers
James
|
|
|
|
|
Hi, I play this game that is DirectX based. It allows you to press Alt+Tab to switch to desktop. At that time the game is minimized, if you press the button in the taskbar the game goes back to fullscreen. When you rightclick the button in the taskbar you get nothing. Now what i want to do is to somehow write a small application so that when the game is minimized i can run some code to make the game maximize in a window(with a caption and all) and so that it doesn't take up the entire screen and i can drag it around. I already tried FindWindow to get the handle and then SetWindowPos() but the game doesn't allow this. What would be the best way to handle this? (and most likely to work)
Many thanks in advance!
Kuniva
--------------------------------------------
|
|
|
|
|
The game probably requires full screen. You may not be able to override that. Check in the DirectX documentation.
Gary R. Wheeler
|
|
|
|
|
Well in my knowledge there's always a way to override things, even if you have to start modifying the directx engine
Thanks anyways
Kuniva
--------------------------------------------
|
|
|
|
|
A directX program must explicitly support windowed mode in order for it to run proprerly. It is possible that your game requires page flipping that is only supported in full-screen exclusive mode, in that case there is no way for you to force it to run in windowed mode.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
In my CMyDocument class I've created a private CObList of some objects. How can I easily get access to this data in a safe way in my View class and dialog, created form my View class? In D.Kruglinski's book there's an example in Chapter 16 but the document there returns a pointer to the private data. But in Deitel's book "C++: How to Program" (Chapter 6.15) the authors do not recommend to return a reference or a pointer to the private data as it destroys the class incapsulation.
Could you give me some piece of advice on this point?
|
|
|
|