O'Reilly logo

Visual C# 2005: A Developer's Notebook by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create Safe Asynchronous Tasks

.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.

How do I do that?

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.

The Reminder application

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.

The Reminder application in progress

Figure 3-25. The Reminder application in progress

Note

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required