Thread problem!
-
Hi all! I have a thread problem in the following code: (This is not the complete code..) Somewhere in a function i have the following implementation: /*****************************************/ // Start of thread HANDLE hThread = CreateThread(...); // Wait until thread ends WaitForSingleObject(hThread, INFINITE); ... /*****************************************/ // Thread procedure DWORD WINAPI ThreadProc(LPVOID lpParam) { // My code ...... ...... SendMessage() <--- The system right here is deadlock (***) return 0; } (***) The SendMessage() function sends message (LVM_INSERTITEM) to a list view control of the window that stared the thread. The problem is that I want the SendMessage() function to work, without removing the WaitForSingleObject() function, or any other "wait" function it might need to be replaced. Thanks for any reply!
-
Hi all! I have a thread problem in the following code: (This is not the complete code..) Somewhere in a function i have the following implementation: /*****************************************/ // Start of thread HANDLE hThread = CreateThread(...); // Wait until thread ends WaitForSingleObject(hThread, INFINITE); ... /*****************************************/ // Thread procedure DWORD WINAPI ThreadProc(LPVOID lpParam) { // My code ...... ...... SendMessage() <--- The system right here is deadlock (***) return 0; } (***) The SendMessage() function sends message (LVM_INSERTITEM) to a list view control of the window that stared the thread. The problem is that I want the SendMessage() function to work, without removing the WaitForSingleObject() function, or any other "wait" function it might need to be replaced. Thanks for any reply!
This will never work !! I suppose the function which creates the Thread is not in another thread than your application ? Thus your application will freeze because of the WaitForSingleObject call. And because of that, it cannot process messages anymore (so it will not respond to user events neither). It is not a good idea to call WaitForSingleObject in the main thread. A better idea would be that, when your secondary thread finishes, it sends a message (an owner defined message) to the main window resulting in calling a specific function that will manage every that needs to be done when your thread finishes.
-
Hi all! I have a thread problem in the following code: (This is not the complete code..) Somewhere in a function i have the following implementation: /*****************************************/ // Start of thread HANDLE hThread = CreateThread(...); // Wait until thread ends WaitForSingleObject(hThread, INFINITE); ... /*****************************************/ // Thread procedure DWORD WINAPI ThreadProc(LPVOID lpParam) { // My code ...... ...... SendMessage() <--- The system right here is deadlock (***) return 0; } (***) The SendMessage() function sends message (LVM_INSERTITEM) to a list view control of the window that stared the thread. The problem is that I want the SendMessage() function to work, without removing the WaitForSingleObject() function, or any other "wait" function it might need to be replaced. Thanks for any reply!
Hello, What you are doing is asking for trouble! The rule of thumb is: never touch the GUI from your worker thread!!!! If you really need to do this, try the following: use
PostMessage()
instead ofSendMessage()
. If you really need to wait for the thread to finish, useMsgWaitForMultipleObjects()
instead ofWaitForSingleObject()
. This way, your app won't freeze because it will process messages. Hope this helps. Behind every great black man... ... is the police. - Conspiracy brother Blog[^]