Thread: Assertion Error with DoModal(), activated from a thread

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    Assertion Error with DoModal(), activated from a thread

    hi, as with the topic, i am a newbie at MFC programming and ii am having trouble with DoModal
    it shows the window but immediately give an assertion error

    http://img186.imageshack.us/img186/9563/assert9dk.jpg

    here's how the application init looks like:
    Code:
    BOOL CAsevApp::InitInstance()
    {
    	time_stop_dlg dlg;
    	m_pMainWnd = &dlg;
    	AfxBeginThread ( th_start_accept, (LPVOID)this );
    	return TRUE;
    }
    over here, time_stop_dlg is a class derived from CDialog, without any changes, but has been linked to IDD_MY_DIALOG_TO_SHOW (as suggested by classwizard)



    this is the thread procedure:
    Code:
    UINT CAsevApp::th_start_accept( LPVOID lpvParam )
    {
        CAsevApp *th_start_accept = (CAsevApp*)lpvParam;
        th_start_accept->process_received_msg(buffer);
       return 0;
    }
    process_received_msg(buffer) is a function member of CAsevApp
    Code:
    void CAsevApp::process_received_msg(char *msg)
    {
    		time_stop_dlg time_stop_dlg_obj;
    		time_stop_dlg_obj.DoModal(); // NIGHTMARE BEGINS HERE!
    }

    a snippet of wincore assertion error:
    Code:
    BOOL PASCAL CWnd::WalkPreTranslateTree(HWND hWndStop, MSG* pMsg)
    {
    	ASSERT(hWndStop == NULL || ::IsWindow(hWndStop));
    	ASSERT(pMsg != NULL);
    
    	// walk from the target window up to the hWndStop window checking
    	//  if any window wants to translate this message
    
    	for (HWND hWnd = pMsg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
    	{
    		CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
    		if (pWnd != NULL)
    		{
    			// target window is a C++ window
    			if (pWnd->PreTranslateMessage(pMsg))
    				return TRUE; // trapped by target window (eg: accelerators)
    		}
    
    		// got to hWndStop window without interest
    		if (hWnd == hWndStop)
    			break;
    	}
    	return FALSE;       // no special processing
    }
    http://img186.imageshack.us/img186/7370/retrn0wk.jpg

    using
    vc6.0 sp6

    your assistance please!
    Last edited by hanhao; 07-07-2005 at 02:07 PM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I could have sworn there was a Windows programming forum for these types of questions. Oh, wait...yup, there it is.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    opps,
    my bad
    i'll post there then

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Does the dialog window function correctly in the main thread?

    Kuphryn

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    Quote Originally Posted by kuphryn
    Does the dialog window function correctly in the main thread?

    Kuphryn
    yes

    i tried to instead of creating a dialog box directly from the thread, i post a message to main window

    ::PostMessage(this->m_pMainWnd->m_hWnd, WM_MAKE_TIME_STOP_DLG, (WPARAM)broken_msg_obj.parm2,
    and in my main thread have

    LRESULT CAsevApp:nWM_MAKE_TIME_STOP_DLG(WPARAM wParam, LPARAM lParam)
    but it doesnt work
    i believe because postmessage(hwnd <===
    is specified wrongly
    can anyone please point me to the right thread?

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Make sure you include the message handler in the message map and .h file too.

    For simplicity: m_pMainWnd->PostMessage()

    Kuphryn

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    Quote Originally Posted by kuphryn
    Make sure you include the message handler in the message map and .h file too.

    For simplicity: m_pMainWnd->PostMessage()

    Kuphryn
    thanks for your reply
    but i got an assertion error
    btw i m doing this from a thread
    it doesnt matter right?

    - this is done from within a thread
    - the application is windowless

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Moved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    114

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> i am a newbie at MFC programming
    You need to just forget about using threads. 99% of the time, you can accomplish what you need without them anyways.

    Multi-threading is very hard. Using multiple threads within an application framework like MFC is even harder if you don't know how MFC works inside and out.

    Start working on eliminating all threads from your app. If you don't know how do accomplish something without the extra threads, start a new post explaining your situation and we'll tell you how to do it without threads.

    gg

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    anyway i got the solution
    thanks to all who replied

    dialog creation in a thread is no good
    it has to be done via postmessage

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  2. Multithreading
    By Cela in forum Windows Programming
    Replies: 13
    Last Post: 01-15-2003, 03:02 PM
  3. Your Best thread and your most stupid thread ??
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-03-2003, 07:41 PM
  4. MFC Controls and Thread Safety :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 12-06-2002, 11:36 AM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM