Click here to Skip to main content
14,027,472 members
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 17 Aug 2004

MFC UI Threads

, 17 Aug 2004
Rate this:
Please Sign up or sign in to vote.
A simple UI thread example.

Image of demo application


This article describes a quick UI thread implementation. Running the demo project will create two modal dialog windows. They will initially appear right on top of each other. Move the top window off to the side so you can see both windows.

Each dialog is run in a different thread, and each dialog will maintain a separate message loop. This behavior can be seen by clicking on the Lock button for each thread. After clicking on "Lock", the thread will attempt to acquire access to the named lock "ThreadLockExample". If the thread gets access to the lock, a "Got Lock" message will be displayed. If the thread is unable to get access to the lock, then a "Waiting" message will be displayed. The locking behavior seen in these two dialogs could not occur without using UI threads. Why? There are 2 reasons:

  1. If two dialogs are running within 1 thread, then waiting for the lock would halt both threads.
  2. You could not create a separate dialog in a worker thread. Worker threads do not have a message loop.

Source Code

  • Lock.h/cpp

    Manages a single lock, and abstracts away implementation details from user.

  • UIThread.h/cpp

    Implementation of a UI thread.

  • ThreadExDlg.h/cpp

    Thread dialog.

  • ThreadEx.h/cpp

    Thread example main app.

Code Notes

Creating a thread

To create a user interface thread, create an instance of a class derived from CWinThread. Then, call the method CreateThread. See UIThread.h/cpp and ThreadEx.cpp for more information.

class UIThread : public CWinThread
BOOL CThreadexApp::InitInstance()
    // Create a thread to generate a maintain a instance of CThreadexDlg.

Create a mutex (lock)

To create a lock, just call CreateMutex(...). See Lock.h/cpp.

CDMLock::CDMLock(const CString & name)
    mutex = CreateMutex(NULL, 0, name);

Acquire lock (down)

Call WaitForSingleObject(...). You can optionally pass a timeout value. When you have waited longer than the specified time, the function WaitForSingleObject will return WAIT_TIMEOUT. If the lock is acquired successfully, it will return WAIT_OBJECT_0.

// Get lock. If timeout expires than return fail.
bool CDMLock::Lock(DWORD timeout)
    if (mutex) {
        if (WaitForSingleObject(mutex, timeout) == WAIT_OBJECT_0) 
            return true;
    return false;

Release Lock (up)

// Release lock.
bool CDMLock::Unlock()
    if (mutex) {
        if (ReleaseMutex(mutex))
            return true;
    return false;

CDMLock (Reusable)

Most of the code included in the download for this article is demo code. But the class CDMLock (lock.h/cpp) is kind of a nice class to have around. While working with locks really is not that difficult, it is always nice to have the details abstracted away. CDMLock is pretty simple to use. Just pass a string into the constructor to create a Named Lock. Then call Lock to acquire access to the lock. Call Unlock to release the lock. For an example of the usage of CDMLock, look at ThreadExDlg.cpp.

The CDMLock class can also be used for two processes running on the same computer, not just two threads in the same process.

class CDMLock  
    CDMLock(const CString & name);
    virtual ~CDMLock();
    bool Unlock();
    bool Lock(DWORD timeout = INFINITE);
    HANDLE mutex;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionEasy Improvement Pin
Mark41212-Mar-18 17:25
memberMark41212-Mar-18 17:25 
QuestionNot responding Pin
lavoiekeven26-Sep-12 3:05
memberlavoiekeven26-Sep-12 3:05 
GeneralInteresting! Pin
WREY18-Aug-04 10:07
memberWREY18-Aug-04 10:07 
I remember those days well, when I could have used something like this in getting me started towards understanding multithreading.

I'm sure others will find it useful although it lacks some of the other important information that should be included when working with threads. For example, your article shows the sample to be applicable for both Win32, and MFC, when in fact, creating and working with threads in Win32, is very different than working with threads in MFC. To start out with, their prototypes are different and there are certain API's one would use specifically when using the Win32 version instead of the MFC version (and vice-versa).

Also, there were some other locking mechanisms you omitted and failed to address (plus your sample deadlocked when I tested it more comprehensively).

Suggestion: If you were to have labelled it for Beginners, it might have cleared up ideas some may have had about it being usable in a more widespread manner.


Fortes in fide et opere!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 18 Aug 2004
Article Copyright 2004 by telemacher
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid