Visual Basic Concepts

Interrupting Background Processing

Your application may utilize long background processing to accomplish certain tasks. If this is the case, it is helpful to provide the user with a way to either switch to another application or interrupt or cancel the background task. The Windows operating environment gives users the first option: switching to another application by using the ALT+TAB key combination, for instance. You can provide the other options by writing code that responds when a user either clicks a cancel button or presses the ESC key.

In considering how to implement this in your application, it's important to understand how tasks from various applications are handled by the operating system. Windows is a preemptively multitasking operating system, which means that idle processor time is efficiently shared among background tasks. These background tasks can originate from the application the user is working with, from another application, or perhaps from some system-controlled events. Priority is always given to the application that the user is working with, however. This ensures that the mouse and keyboard always respond immediately.

Background processing can be placed into two categories: constant and intermittent. An example of a constant task would be copying a file from a server. Periodically updating a value would be an example of an intermittent task. Both types of tasks can be interrupted or canceled by the user. However, because background processing is usually a complex matter, it is important to consider how these tasks are initiated in the first place. The topic "Allowing Users to Interrupt Tasks" later in this chapter describes these considerations and techniques.