Convert modal dialogs to modeless | CodeGuru

Convert modal dialogs to modeless

Many postings to CodeGuru and similar discussion groups are questions about modeless dialogs and how to keep them from blocking the parent thread. I use a user interface thread to start a conventional modal dialog that behaves modeless-ly and it’s so minimalist and easy I thought others may be interested… Instructions In your header file […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 16, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Many postings to CodeGuru and similar discussion groups are questions about modeless
dialogs and how to keep them from blocking the parent thread. I use a user interface thread
to start a conventional modal dialog that behaves modeless-ly and it’s so minimalist and
easy I thought others may be interested…

Instructions

  1. In your header file define a CWinThread-derived class…
    class CDialogThread : public CWinThread
    {
     DECLARE_DYNCREATE(CDialogThread)
     CDialogThread() {};
     virtual BOOL InitInstance();
    };
    
    
    
  2. Put this in your implementation file (where CSomeDialog is a conventional dialog class defined the usual way).
    IMPLEMENT_DYNCREATE(CDialogThread, CWinThread)
    BOOL CDialogThread::InitInstance()
    {
     CSomeDialog dlg;
     dlg.DoModal();
     return FALSE;
    }
    
    
    
  3. To create an instance of your (now-modeless) modal dialog, do this…
    AfxBeginThread ( RUNTIME_CLASS(CDialogThread) );
    
    
    
  4. You can end the dialog the normal way by calling CDialog::EndDialog(),
    CDialog::OnCancel() or CDialog::OnOK(). All those special caveats about
    modeless dialogs (like “don’t call EndDialog”) no longer apply with this
    approach.

  5. One point to note is the return of ‘FALSE’ from the InitInstance()
    override. This tells MFC that the thread didn’t start successfully, so
    MFC does our cleanup for us! Note that by this time the dialog has already
    returned and we’re through with the thread. This is the same thing MFC-generated
    code does in the InitInstance() of dialog-based applications.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.