SendMessage Hangs
-
Hi, I have 2 threads in my app. one is main application thread which has UI. second is the worker thread. second thread will get some status from a database and send the information to the main thread using SendMessageAPI. One of the parameters in the SendMessage API is a global variable across the threads. After the recieving the message from second thread, first thread will display the error to the user and return the control so that the second thread can monitor the status continuously. But at some point, my app hangs. I suspect that the hang happens in this loop. As the return depends on the response from the main thread (thru user input via messagebox), i could not use SendMessageTimeout API in this case. 1. Is it because of the global variable usage in SendMessage function? Can you please let me know how can i come out of this problem. Any help is appreciated.
Selva
-
Hi, I have 2 threads in my app. one is main application thread which has UI. second is the worker thread. second thread will get some status from a database and send the information to the main thread using SendMessageAPI. One of the parameters in the SendMessage API is a global variable across the threads. After the recieving the message from second thread, first thread will display the error to the user and return the control so that the second thread can monitor the status continuously. But at some point, my app hangs. I suspect that the hang happens in this loop. As the return depends on the response from the main thread (thru user input via messagebox), i could not use SendMessageTimeout API in this case. 1. Is it because of the global variable usage in SendMessage function? Can you please let me know how can i come out of this problem. Any help is appreciated.
Selva
Please post some code: from your worker thread when you send the message and from the main thread where you handle this message. By 'hangs' you mean that your UI freeze completely and you can't do anything anymore with your app ?
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
Please post some code: from your worker thread when you send the message and from the main thread where you handle this message. By 'hangs' you mean that your UI freeze completely and you can't do anything anymore with your app ?
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++Hi, Thanks for the reply. Code will look like below. int g_nInt; Main thread LRESULT CMyDlg::OnProcessMsg(WPARAM wparam, LPARAM lparam) { Display Message box ....... Get result from user. return 0; } Worker thread { get status from DB. GetMainWnd()->SendMessage(PROC_MSG, wp, g_nInt); ..... GetMainWnd()->SendMessage(PROC_MSG2, wp, lp); ......... } Yes. My main app window looks like freezed (continuous even after an hour). Thanks in advance.
Selva
-
Hi, Thanks for the reply. Code will look like below. int g_nInt; Main thread LRESULT CMyDlg::OnProcessMsg(WPARAM wparam, LPARAM lparam) { Display Message box ....... Get result from user. return 0; } Worker thread { get status from DB. GetMainWnd()->SendMessage(PROC_MSG, wp, g_nInt); ..... GetMainWnd()->SendMessage(PROC_MSG2, wp, lp); ......... } Yes. My main app window looks like freezed (continuous even after an hour). Thanks in advance.
Selva
SendMessage is a blocking function: it will only return once the message has been processed by the other window. In your case, it will return only after the user has closed the MessageBox. You didn't provide enough code for me to verify this but you probably have a deadlock: your worker thread is waiting for the UI thread (because of the SendMessage function) which is in turn waiting for the worker thread. If your design allows it, try with PostMessage instead of SendMessage: PostMessage puts the message in the message queue of the window and returns immediately.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
SendMessage is a blocking function: it will only return once the message has been processed by the other window. In your case, it will return only after the user has closed the MessageBox. You didn't provide enough code for me to verify this but you probably have a deadlock: your worker thread is waiting for the UI thread (because of the SendMessage function) which is in turn waiting for the worker thread. If your design allows it, try with PostMessage instead of SendMessage: PostMessage puts the message in the message queue of the window and returns immediately.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++Hi, Thanks for the reply. Main thread won't be waiting for the worker thread.. only if the second request from the worker thread comes, it will be processing. But i too suspect that its because of the deadlock. But my doubt is, whether passing the global variable (which will be accessed by both the threads) causes this hang. any way storing the value in global variable will happen with proper locks from both the threads. But whether passing this as the parameter to the SendMessage causes any problem? Thanks in advance.
Selva
-
Hi, Thanks for the reply. Main thread won't be waiting for the worker thread.. only if the second request from the worker thread comes, it will be processing. But i too suspect that its because of the deadlock. But my doubt is, whether passing the global variable (which will be accessed by both the threads) causes this hang. any way storing the value in global variable will happen with proper locks from both the threads. But whether passing this as the parameter to the SendMessage causes any problem? Thanks in advance.
Selva
Sorry but your question is not really clear. Please post some more relevant code instead of explaining what your code does. Anyway, why do you pass the value in the message ? It is globally available so, there's no need to pass it in the message. But accessing this variable from two threads won't make your UI hang (unless you are using critical section to access it, then the problem might come from that).
SelvaKr wrote:
any way storing the value in global variable will happen with proper locks from both the threads.
I'm not sure what that means but if you are using critical section, then this is probably the problem. Did you try with PostMessage as I told you ? What happened ?
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
Sorry but your question is not really clear. Please post some more relevant code instead of explaining what your code does. Anyway, why do you pass the value in the message ? It is globally available so, there's no need to pass it in the message. But accessing this variable from two threads won't make your UI hang (unless you are using critical section to access it, then the problem might come from that).
SelvaKr wrote:
any way storing the value in global variable will happen with proper locks from both the threads.
I'm not sure what that means but if you are using critical section, then this is probably the problem. Did you try with PostMessage as I told you ? What happened ?
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
Hi, Thanks for the reply. Code will look like below. int g_nInt; Main thread LRESULT CMyDlg::OnProcessMsg(WPARAM wparam, LPARAM lparam) { Display Message box ....... Get result from user. return 0; } Worker thread { get status from DB. GetMainWnd()->SendMessage(PROC_MSG, wp, g_nInt); ..... GetMainWnd()->SendMessage(PROC_MSG2, wp, lp); ......... } Yes. My main app window looks like freezed (continuous even after an hour). Thanks in advance.
Selva
Perhaps your problem is using the MainWnd object across threads (you didn't say if this is MFC or not, but I suspect it is). It would be safer to either pass the hWnd of your main window in the thread parameters when you start the thread, or to use GetSafeHwnd() when you send your message. For example:
Worker thread
{
get status from DB.
::SendMessage(GetMainWnd()->GetSafeHwnd(),PROC_MSG, wp, g_nInt)
.....
::SendMessage(GetMainWnd()->GetSafeHwnd(),PROC_MSG, wp, g_nInt)
.........
}Also, you say the code will "look like" below. Post your actual code, including how you start the thread. Hope that helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193