- Why Do We Need Background Processing?
- Running a Process on Another Thread
- The Single-Threaded Example
- Creating a Multithreaded Version
- Working with the User Interface from Another Thread
- Conclusion
Running a Process on Another Thread
The term background process indicates that we want the process to run without blocking (pausing or "hanging") our main process, which is the UI. In the world of the Windows OS, accomplishing that task involves threads. Threads represent the CPU's view of your program, as a series of commands that are executed in sequence. A single processyour application or another program such as Microsoft Wordwill consist of one or more "threads of execution." When combined with the thread-scheduling and multitasking features of the operating system, multiple threads produce the appearance of multiple series of commands being executed concurrently, even on a single-processor system.
Writing a program so that it uses more than a single thread to run is called multithreading, and this is a very powerful tool. Before Visual Basic .NET, multithreading was not a normal activity for most Visual Basic programmers because of the complexity and the limitations of Visual Basic 6.0 and earlier. Even when people managed to use multiple threads from their VB code, the results were usually unstable and multithreading was not often recommended. With the arrival of .NET, threading is one of the many features provided not by the Visual Basic. NET language itself, but instead as part of the runtime that underlies all of the .NET languages. As a result, Visual Basic .NET has the same multithreading capabilities as C#, and manipulating threads is no longer an unstable programming technique. It is worth noting, however, that multithreading is still quite complex to accomplish successfully.