Easy Multithreading in ATL Windows Applications

At my work, I'm the GUI developer. Our application implies a lot of computations to do. The end user is expected to watch some Flash clips until the work is done.

I chose the ATL GUI framework. Because I'm too lazy to constantly call the Win API's functions, I created the C++ template class to do this job for me. Then, I decided that the code I wrote could be extremely helpful when creating any ATL GUI applications.

The idea is to exploit the old-school windows messages to notify the owner window that the job is done. I know this idea sounds too complicated when programming in pure Win32, but it can be implemented with just a few lines of C++ code when using ATL windows classes. I also exploited the useful yet poorly understood C++ language feature—pointers to C++ member functions.

Using this class is extremely simple. Just add Async.h to your project, inherit your main window class from the CAsync<CMyWindowWindowClass>, add the CHAIN_MSG_MAP macro to the window's message map, and you are ready to execute your class functions asynchronously.

In the attachment, you'll find the demo project. I wrote and debugged this simple application while chilling out at home just to demonstrate the possible use for this technique. That's why the application only moves the progress bar, not actually doing any useful work. I tried to comment the code well to make it easier to read and understand.

Pros

Easy to use. Good for any network clients. No WIN32 Wait functions. Readable C++ code.

Cons

Can't terminate hung jobs (could be implemented easily). Suitable only for GUI applications because it depends on window messages. Can only pass one or more Variant values to the thread (however, you may use any data stored in your class data members). Besides, you should think about the lifetime of the thread and the window object: If the window object is destroyed but the application is still active and some thread[s] are active, you'll most likely get an access violation when accessing the class members from the thread.



About the Author

Konstantin Go

I work as a software developer since 2000. I worked on all kinds of software projects including (but not limited to) scientific, database, ERP, testing, enterprise-level windows management and security. Currently I'm developing a game for WiiWare platform.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds