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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds