Rob P
Rob P

Reputation: 41

Why does CFileDialog::DoModal() Hang?

I have developed a fairly large C++ program in VS 6.0 on a Win XP platform, and have now migrated to a new machine running Win 7 (still running VS 6.0). The code includes a function to instantiate and run a CFileDialog object to find and open an ASCII file with a specific extension from a specific initial directory. But now, the program hangs on the line

if (t1.DoModal()==IDOK)

...where t1 is the CFileDialog instance. To investigate why the standard CfileDialog class stopped working, I created a separate test project in VS 6.0 with a simple dialog with one button, containing this code:

void CFileDialogTestDlg::OnOpenFileDialogButton() 
{
  CFileDialog t1(true);
  if(t1.DoModal()==IDOK)
  {
    CString s3=t1.GetPathName();
    MessageBox(s3);
  }
}

This test works fine and displays a useable file dialog. I can also duplicate what I want in my large project in terms of initial directory,etc by modifying the m_ofn members of t1.

But putting this code into my large project (ie modifying the relevant button in it) still hangs on the DoModal() line. It seems unproductive trying to trace into a standard MS class, the internals are impossible to understand in a reasonable timeframe.

When I increased stackspace for my test project to match my large project (400MB), I reproduced the hanging behaviour identical to the large project.

Can anyone explain why increasing stackspace should affect file dialog execution in this way, and is there a way around the problem, bearing in mind I need the large stackspace to avoid completely rewriting my project?

Upvotes: 4

Views: 1932

Answers (2)

JBRWilkinson
JBRWilkinson

Reputation: 4875

Use PostMessage() API to send commands from any thread to the thread that owns the modal dialog. It needs to be the owning (and blocking) thread that ultimately receives the command to accept/cancel the dialog so that it returns from its message pump routine.

If you install the Windows debug symbols, you can see the full call stack of your blocking thread in a debugger.

Upvotes: 2

tzerb
tzerb

Reputation: 1433

I'm not sure the stack is your problem. It's been a while but I seem to recall common modals hanging if you access them from the wrong thread.

Upvotes: 2

Related Questions