Using background threads for long processes in MFC
It’s not easy to write a Windows program that takes a long time to do something and yet still seems responsive to the user. Consider that you’ve got a single task that needs to be finished:
- The task is going to take too long to simply use an hourglass.
- Progress reporting (percentages, for example) is relatively infrequent.
This article will lay out a couple of different ways to solve this problem. It’s broken down into reasonably-sized sections:
- Introduction (this post; 15 Apr 2003 11:20)
- Sidebar: The Task (15 Apr 2003 11:30)
- Foreground Thread 1 (15 Apr 2003 11:41)
- Foreground Thread 2 (15 Apr 2003 11:42)
- Foreground Thread 3 (15 Apr 2003 11:43)
- Background Thread 1 (15 Apr 2003 11:51)
- Background Thread 2 (15 Apr 2003 11:52)
- Background Thread 3 (15 Apr 2003 11:53)
- Background Thread 4 (15 Apr 2003 11:54)
Test Harness
We’ll start with a test program. The simplest thing to do is to fire up Visual C++ and generate a brand-new MFC dialog application. We can then put buttons on it, so that we can experiment with different ways to schedule the task.