locked
UpdateAllViews fails when called RRS feed

  • Question

  • Hi all,
        I created multiple views on a SDI project. The shared cdocument is the default cdocument object. I also a created serial port class, with the
    collected data stored in cdocument object. To access the cdocument, i got help from: http://support.microsoft.com/kb/108587. How To Get Current CDocument or CView from Anywhere)
    However when i call cdocument updateAllViews, i got a pop msg; Debug Assertion Failed!
    Also i cant see a global variable created in cdocument thru other views object.

    Please advise me!
     


    adam
    Friday, June 19, 2009 11:59 AM

Answers

  • You should read the comment in the MFC code near the ASSERT.  (It is so important that it is the longest comment in the entire MFC source!)

    MFC does not permit you to access windows from a thread that did not create the window.  Since UpdateAllViews does (indirectly) access windows, you cannot call it from a secondary thread.  What you must do is PostMessage from the secondary thread, and let the message handler in the main thread do the window updates.  An example is here:
    http://vcfaq.mvps.org/mfc/index.htm

    Friday, June 19, 2009 3:51 PM
  • You should not ignore an ASSERT, its given to help you find potential bugs.
    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Friday, June 19, 2009 2:43 PM

All replies

  • >> However when i call cdocument updateAllViews, i got a pop msg; Debug Assertion Failed!

    Can you click "Retry" and see where it asserts.
    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Friday, June 19, 2009 1:37 PM
  • 1. Show the code that you use to get the document pointer and call UpdateAllViews.
    2. Show the MFC code that asserts.
    Friday, June 19, 2009 1:38 PM
  • Hi there,
                  Thank u all for telling me where to c. The error msg show: Debug assrts Fails. I changed to release mode and all works fine. VERY CARELESS OD ME! Anyway, i will still explain. SerialEventManager function is a function called by a thread within the serial port object. The thread does a endless while loop, that check the serial port handles. 

          

    void

     

    SerialEventManager(uint32 object, uint32 event)

    {

     

    char *buffer;

     

    int size;

    Tserial_event *com;

    com = (Tserial_event *) object;

     

    if (com!=0)

    {

     

    switch(event)

    {

     

    case SERIAL_CONNECTED :

     

    break;

     

    case SERIAL_DISCONNECTED :

     

     

    break;

     

    case SERIAL_DATA_SENT :

     

     

     

     

    if(level >= 9)

    level = 0;

    level++;

    tess = sendstring[level];

     

    break;

     

    case SERIAL_RING :

     

     

    break;

     

    case SERIAL_CD_ON :

     

     

    break;

     

    case SERIAL_CD_OFF :

     

     

    break;

     

    case SERIAL_DATA_ARRIVAL :
                                           
                                           com->doc->UpdateAllViews(NULL );  //Error happens here

    }
    }


    }
    As for the global variable in cdocument... i still cant read them from other object.HELP!


    adam
    Friday, June 19, 2009 2:32 PM
  • You should not ignore an ASSERT, its given to help you find potential bugs.
    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Friday, June 19, 2009 2:43 PM
  • You should read the comment in the MFC code near the ASSERT.  (It is so important that it is the longest comment in the entire MFC source!)

    MFC does not permit you to access windows from a thread that did not create the window.  Since UpdateAllViews does (indirectly) access windows, you cannot call it from a secondary thread.  What you must do is PostMessage from the secondary thread, and let the message handler in the main thread do the window updates.  An example is here:
    http://vcfaq.mvps.org/mfc/index.htm

    Friday, June 19, 2009 3:51 PM