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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds