.NET provides extensive support for multithreaded applications without requiring you to manage
the threads. In .NET 2.0 the support for safe asynchronous tasks is
greatly enhanced with the addition of the
BackgroundWorker object, which allows you to
work safely in a second thread while maintaining user interface control
(for updates and cancellation) in your main thread. There is no need to
spawn threads explicitly or to manage resource locking.
To demonstrate how this works, you'll create a small Windows application that provides a reminder after a specified number of seconds have passed, as shown in Figure 3-24.
Figure 3-24. The Reminder application
You'll write the program so that when you click Start, the Start button and text boxes are disabled, and the Cancel button is enabled. While the timer is ticking down, a progress bar will show what percentage of time has expired, as shown in Figure 3-25.
Figure 3-25. The Reminder application in progress
Be sure to set a reasonable default value in the txtSeconds control so that the user can click Start without throwing an exception.
If you click Cancel, a cancel message is displayed. When the time has elapsed (or the timer is cancelled), the text is displayed, cancel is disabled, and the ...