Click to See Complete Forum and Search --> : CreateDialogIndirect hangs on CWinThread::InitInstance


gigizhang
June 15th, 2007, 07:40 PM
Hey,

I'm new to multithreading.

I created a class inherit from CWinThread called CMyUIThread, and also a dialog called CMyInfoDlg which has a text and a progress ctrl. I would to show modeless dialog in new created thread. Below is partial code:

BOOL CMyUIThread::InitInstance()
{
// TODO: perform and per-thread initialization here
// NOTE: the memory allocated below is freed by CMyInfoDlg::OnDestroy (it deletes itself)
m_pDlg = new CMyInfoDlg(CWnd::FromHandle(m_hwndParent));
VERIFY( m_pDlg->Create(IDD_THREADUI_DIALOG, CWnd::FromHandle(m_hwndParent)) );
g_hwndProgress = m_pDlg->GetSafeHwnd();
::ShowWindow(g_hwndProgress, SW_SHOW); // show window for the first time
return TRUE;
}

In my main frame, I did the following

m_pThread = new CGSUIThread(m_hwndParent);
m_pThread->m_bAutoDelete = FALSE;
VERIFY( m_pThread->CreateThread() );

OK. In this case, it works fine.

However, when I merge it into another big module which has main frame, problem came: it hangs on: VERIFY( m_pDlg->Create(IDD_THREADUI_DIALOG, CWnd::FromHandle(m_hwndParent)) ); when I tracked down, it is acutally hangs on CreateIndirectDialog. I dont' know why it's like that. I tried to add "Sleep(1000) before the creation, still same problem.

Anyone knows why? Please help. SOS...

zerver
June 18th, 2007, 09:19 AM
Hi!

MFC message loop runs in a single thread. It is not thread-safe.

You should not update the GUI from anything else than the 'main' application thread.

However, you can post a message to the 'main' thread to tell it that it should display this dialog.

Also, if you create a 'global' dialog (parent=NULL) from your thread that should be OK as well.

http://www.codeguru.com/forum/archive/index.php/t-309988.html

gigizhang
June 18th, 2007, 06:35 PM
Thank you zerver for the nice reply. Very helpful.

Yes! You are right, after change the parent window to NULL, the dialog box shows up.

Actually, what I want is a really sensitive progress bar which runs in different thread with work thread.

I referred below articles and use the first one as my solution.

http://www.codeguru.com/forum/archive/index.php/t-298907.html
http://codeguru.earthweb.com/cpp/w-d/dislog/progressindicators/article.php/c1999/#more


What I'm still wondering is: when work thread does something big like loading a big project, how should I control the progress bar? Since I don't know how long it will take. If possible, can you give me some hint?

zerver
June 19th, 2007, 08:37 AM
Have you ever seen the "Preparing to copy..." dialog in Windows Explorer?
It tries to figure out how long the copying will take by calculating the total size of all files.
This is a big mistake, because it makes the copy operation take much longer time.

So, if it is complicated or takes time, do not try to figure out how long it will take :D

Instead, use something simple to indicate that there is ongoing work.

You could use an animation or rapidly moving progress bar that "loops".

Regards / Z

gigizhang
June 20th, 2007, 11:39 AM
I think I misunderstood. I've thougt to have a progress bar in a different thread is able to make some fake progress which will end when I tell them to finish. :)

Guess what, I'm using a tricky right now. After I create a global dialog box (the one with progress bar), I set the parent of the dialog as the one I originally what to set, I can not do that using create because of the message thing. I'm not sure if it's a good idea, but looks like it works for me.

Thanks again zever, I learned a lot.