当我在一个线程里动态创建了一个对话框
我怎么样才能在另外一个线程里引用该对话框。

解决方案 »

  1.   

    你可以用mutex,或semaphore来完成,因为这两种操作可以实现不同进程的调用,在两个线程中就更没问题了。
      

  2.   

    我试过用一个全局指针不行,调试通不过,
    调试出现下列问题:
    ASSERT((p = pMap->LookupPermanent(m_hWnd)) != NULL ||
    (p = pMap->LookupTemporary(m_hWnd)) != NULL);
    ASSERT((CWnd*)p == this);   // must be us // Note: if either of the above asserts fire and you are
    // writing a multithreaded application, it is likely that
    // you have passed a C++ object from one thread to another
    // and have used that object in a way that was not intended.
    // (only simple inline wrapper functions should be used)
    //
    // In general, CWnd objects should be passed by HWND from
    // one thread to another.  The receiving thread can wrap
    // the HWND with a CWnd object by using CWnd::FromHandle.
    //
    // It is dangerous to pass C++ objects from one thread to
    // another, unless the objects are designed to be used in
    // such a manner.不知道用mutex,或semaphore该怎么做
      

  3.   

    如果你只是想能过全局指针来操作对话框类对象的成员变量,那是可以的。但你不能用这个指针来调用与窗口操作有关的函数。如果想进行后一种操作,可以试试这样:
    HWND hwnd=::FindWindow(NULL,"dialog's caption");
    CDialog* pDialog=(CDialog*)CWnd::FromHandle(hwnd);
    pDialog->MoveWindow(……);
      

  4.   

    在线程中CMyDialog * pMyDialog=new CDialog;
    在另外的线程中if (pMyDialog) { do }
      

  5.   

    由于我是创建的对话框类型是Child,所以我不能通过::FindWindow来获得该对话框句柄,另外,我通过 pthread = AfxBeginThread(outputthead,
       &theadpart,
       THREAD_PRIORITY_LOWEST);
    中theadpart结构变量来传递对话框的句柄,这样能传过去,但通过FromHanle来得到该对话框的指针后,通过该指针来调用UpdateData(false), 调试不出错,但就是对话框里的数据没有被更新。