Solved

CWinThread and CDialog - getting them work together

Posted on 2008-06-25
7
1,901 Views
1 Endorsement
Last Modified: 2013-12-14
Hello,

I have created a new thread - that works perfect. Now I want to display a CDialog in this thread. In a Thread Montor a new thread with the name of the dialog is shown but i can not see the dialog. I have called the dialog without a thread - that worked. That have I done wrong?

Below some code which may help.

Greedings Sonja
// Call of the thread

//--------------------
 

  CMyThread * MyThread = new CMyThread();

  MyThread ->CreateThread(CREATE_SUSPENDED);

  MyThread ->m_Parameter = Parameter;

  MyThread ->m_pDialog = pDialog;

  MyThread ->ResumeThread();
 

// MyThread.cpp - class CMyThread : public CWinThread

//--------------
 

int MyThread::Run()

{

  //pDialog initialized to NULL in the constructor of CMyWnd class

   CMyDialog* pDialog = new CMyDialog();

   //Check if new succeeded and we got a valid pointer to a dialog object

   if(pDialog != NULL)

   {

      BOOL ret = pDialog->Create(IDD_MYDIALOG,NULL); // <- In the debugger it stops here

      if(!ret)   //Create failed.

         AfxMessageBox("Error creating Dialog");

      pDialog->ShowWindow(SW_SHOW);

   }

   else

      AfxMessageBox("Error Creating Dialog Object");

  return 0;

}

Open in new window

1
Comment
Question by:Sonja_M
7 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 21866855
You should avoid to open a dialog in a different thread
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 21867022
There are two types of thread.  A worker thread (does NOT support windows) and a UI thread (does support windows).
Which type are you using?  I suspect the first.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 21873986
>> int MyThread::Run()

Don't forget that when Run() returns, the thread ends.  So your thread will only be around for a split second.  

Generally it is a good idea not to override CWinThread::Run().  A better place to put your code is in InitInstance().  Add this function to your class and move the code to create the dialog into it.  This should resolve the problem.

HTH
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:mrwad99
ID: 21874527
OK this works.  The key is in setting CWinThread::m_pMainWnd to be your dialog:


BOOL MyThread::InitInstance()

{

	CMyDialog* pDialog = new CMyDialog();

	BOOL ret = pDialog->Create(IDD_MYDIALOG,NULL); 
 

	m_pMainWnd = pDialog;

	

	m_pMainWnd->UpdateWindow();

	m_pMainWnd->ShowWindow( SW_SHOW );
 

	return CWinThread::InitInstance();

}

Open in new window

0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 499 total points
ID: 21874798
>> The key is in setting CWinThread::m_pMainWnd to be your dialog:

Sorry but that is not true, I made a mistake.  The key is in returning TRUE (not CWinThread::InitInstance(), which returns FALSE, thereby shutting down the thread) from InitInstance().  You don't have to do anything with m_pMainWnd.

Revised code follows.
BOOL MyThread::InitInstance()

{

        CMyDialog* pDialog = new CMyDialog();

        BOOL ret = pDialog->Create(IDD_MYDIALOG,NULL); 

        

        pDialog->UpdateWindow();

        pDialog->ShowWindow( SW_SHOW );

 

        return TRUE;

}

Open in new window

0
 

Author Closing Comment

by:Sonja_M
ID: 31470621
Thank you! Now it is working! Problem solved.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 21876669
Glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.Net Errors Service Crashing 5 112
pre4 challenge 19 94
Line meaning 9 81
unix example issues 18 73
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now