Click here to Skip to main content
15,887,434 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Multi Threaded ::ShowWindow(...) Pin
«_Superman_»22-Apr-13 17:07
professional«_Superman_»22-Apr-13 17:07 
QuestionRe: Multi Threaded ::ShowWindow(...) Pin
David Crow23-Apr-13 4:53
David Crow23-Apr-13 4:53 
AnswerRe: Multi Threaded ::ShowWindow(...) Pin
«_Superman_»23-Apr-13 17:35
professional«_Superman_»23-Apr-13 17:35 
GeneralRe: Multi Threaded ::ShowWindow(...) Pin
David Crow24-Apr-13 2:07
David Crow24-Apr-13 2:07 
Questionget all domains form adsi forest Pin
venkatesh5286722-Apr-13 0:29
venkatesh5286722-Apr-13 0:29 
QuestionThreads and messages - is this legal? Pin
charlieg21-Apr-13 15:31
charlieg21-Apr-13 15:31 
AnswerRe: Threads and messages - is this legal? Pin
pasztorpisti21-Apr-13 19:28
pasztorpisti21-Apr-13 19:28 
GeneralRe: Threads and messages - is this legal? Pin
charlieg22-Apr-13 4:34
charlieg22-Apr-13 4:34 
All good points. I know about SendMessage / PostMessage, and I've read many articles documenting why you need to be very careful sending messages in mfc. I'll see if I can come up with a description that isn't worse than I've already written Smile | :) . Note that this basic architecture was inherited, so I make no claim as to its validity. It's an approach, and it has worked. However in writing this out, I think I've discovered one issue...

- The worker thread is a child of the CWinApp application. It exists for the lifetime of the application.

- Window structure: CWinApp -> CFrameWnd -> Main Dialog -> many more dialogs. The frame object serves as the persistent object of the application. There are times when the main dialog may go away, so the frame object maintains application context. Normally, the CFrameWnd will instantiate the Main Dialog.

The frame object is created like so, its instance is saved away for the application:

C++
CFrameWnd*	pFrameWnd = new CFrameWnd;
m_pMainWnd = pFrameWnd;				// This identifies the application's main wnd


- Every dialog created registers with the frame object its instance (using this). This is done so that when a general message is posted to the frame, the frame then echoes it out to all existing dialogs.

- The timer thread posts the timer message indirectly by invoking a public helper method from the frame object: pFrame->BroadcastTimerEvent(); The fact that a worker thread is using an object from the UI side of application CANNOT be a good thing. Comments?

In this BroadcastTimerEvent method, we finally post a message into the message queue:

C++
<pre>Wnd * pWindow;

   pWindow = 	AfxGetApp()->m_pMainWnd;
   if (NULL != pWindow) pWindow->PostMessage(WM_DATA_TIMER);



This effectively queues the message to arrive and be handled by the frame's OnDataTimer handler. Note from above that I mentioned I keep track of all active dialogs. This is done to ease pushing out the timer events to all existing dialogs:
C++
<pre>LRESULT CFrameWnd::OnPanelDataTimer(WPARAM wParam, LPARAM lParam)
{
  if (NULL != m_pClientWnd)  // m_pClientWnd is the main dialog
   {
      if (NULL != m_pClientWnd->m_hWnd)
      {
         // Only send events to registered dialogs with legal windows.  Some dialogs exist w/o
         // having yet been completely instantiated.  Send the events in reverse order, as the array
         // list implies a creation/stacking last in the newest order.

	for(SHORT j = m_pDialogsInUse.GetSize() - 1; j >= 0; j--)
	  if (m_pDialogsInUse[j]->m_hWnd != NULL)
             m_pDialogsInUse[j]->PostMessage(WM_DATA_TIMER);	
      }
   }

return 0;
}


So, I finally come back to my race condition issue - the timer thread fires and starts to push messages out as described. The user happens to press the dialog exit button. Standard button, invokes EndDialog() and terminates. Windows now goes about tearing down that dialog. This dialog has been created on the stack, so it's still out there for a brief period of time. This is where I think there might be a problem...
Charlie Gilley
<italic>You're going to tell me what I want to know, or I'm going to beat you to death in your own house.

"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759


GeneralRe: Threads and messages - is this legal? Pin
pasztorpisti22-Apr-13 6:46
pasztorpisti22-Apr-13 6:46 
GeneralRe: Threads and messages - is this legal? Pin
charlieg22-Apr-13 11:20
charlieg22-Apr-13 11:20 
GeneralRe: Threads and messages - is this legal? Pin
PJ Arends22-Apr-13 12:00
professionalPJ Arends22-Apr-13 12:00 
GeneralRe: Threads and messages - is this legal? Pin
charlieg22-Apr-13 11:20
charlieg22-Apr-13 11:20 
GeneralRe: Threads and messages - is this legal? Pin
pasztorpisti22-Apr-13 22:46
pasztorpisti22-Apr-13 22:46 
AnswerRe: Threads and messages - is this legal? Pin
Stephen Hewitt22-Apr-13 20:48
Stephen Hewitt22-Apr-13 20:48 
GeneralRe: Threads and messages - is this legal? Pin
charlieg23-Apr-13 14:17
charlieg23-Apr-13 14:17 
QuestionName Space Vs Include Pin
Bram van Kampen21-Apr-13 14:51
Bram van Kampen21-Apr-13 14:51 
AnswerRe: Name Space Vs Include Pin
pasztorpisti21-Apr-13 19:40
pasztorpisti21-Apr-13 19:40 
GeneralRe: Name Space Vs Include Pin
Bram van Kampen28-Apr-13 0:34
Bram van Kampen28-Apr-13 0:34 
QuestionHiding the folder from the explorer Pin
sarfaraznawaz18-Apr-13 23:59
sarfaraznawaz18-Apr-13 23:59 
AnswerRe: Hiding the folder from the explorer Pin
André Kraak20-Apr-13 8:17
André Kraak20-Apr-13 8:17 
GeneralRe: Hiding the folder from the explorer Pin
Bram van Kampen21-Apr-13 15:19
Bram van Kampen21-Apr-13 15:19 
AnswerRe: Hiding the folder from the explorer Pin
Stefan_Lang22-Apr-13 0:26
Stefan_Lang22-Apr-13 0:26 
Questionhow to start a thread to get webinfo(xml) with chttpfile Pin
haha_c18-Apr-13 22:54
haha_c18-Apr-13 22:54 
AnswerRe: how to start a thread to get webinfo(xml) with chttpfile Pin
Marco Bertschi18-Apr-13 23:25
protectorMarco Bertschi18-Apr-13 23:25 
AnswerRe: how to start a thread to get webinfo(xml) with chttpfile Pin
Malli_S18-Apr-13 23:43
Malli_S18-Apr-13 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.