Dialog not diap[layed when using threads

When I run this code CMyDialog isn't displayed.  Does anyone know why.
Should I make it as a modeless dialog?


UINT myThread(LPVOID param)
{

      CMyDialog dlg;
      dlg.DoModal();

      return 1;
}


void thisTakesTime()
{
      for (int i = 0; i < 10; i++) {
            CString s = "hello";
            if (i == 9)
                  i = 1;      // never leave loop
      }
}


void CTaskbartestDlg::OnGoButton()
{

    CWinThread* thread = AfxBeginThread(myThread, 0);
    thisTakesTime();
}
LVL 1
MothAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mnbmnbConnect With a Mentor Commented:
Becouse you call dialog, your new thread is UI thread. For UI thread you must declare new class.
0
 
MothAuthor Commented:
Adjusted points to 100
0
 
mnbmnbCommented:
For example:

class MyThread: public CWinThread
{
      DECLARE_DYNCREATE(MyThread)
protected:
      MyThread();           // protected constructor used by dynamic creation

// Attributes
public:
      YorDialog yourdialog;
// Operations
public:

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(MyThread)
      public:
      virtual BOOL InitInstance();
      virtual int ExitInstance();
      //}}AFX_VIRTUAL

// Implementation
protected:
      virtual ~MyThread();

      // Generated message map functions
      //{{AFX_MSG(MyThread)
            // NOTE - the ClassWizard will add and remove member functions here.
      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()
};

IMPLEMENT_DYNCREATE(MyThread, CWinThread)

MyThread::MyThread()
{
}

MyThread::~MyThread()
{
}

BOOL MyThread::InitInstance()
{
  //You show your dialog here
}

int MyThread::ExitInstance()
{

      return CWinThread::ExitInstance();
}


You must call your thread as follows:

AfxBeginThread(RUNTIME_CLASS(MyThread));
0
 
MothAuthor Commented:
Great, Thanks
0
All Courses

From novice to tech pro — start learning today.