In This Chapter
Asynchronous Processing without Threads
Lightweight Threading with Thread Pools
Applying the ThreadStatic Attribute
Multithreading in Windows Forms
A lot of excitement has been generated over the discussion of multithreading capability being added to Visual Basic .NET. We know that it is possible to write professional applications without multithreading. The next year will demonstrate the impact, positive or negative, that multithreading will have on Visual Basic .NET applications. It remains to be seen what productivity gains might be attained and what the added costs of development might be.
As you know, you can solve many day-to-day problems without using multiple threads, but you might find threads useful at times, such as if you need an advanced algorithm. Threads can make your applications more responsive , but they will also probably make your applications more difficult to test and debug.
Chapter 14 is designed to help guide you to using threads safely when you need them. Keep in mind, though, that Windows Forms are not considered thread-safe (although I hope Windows Forms controls will be thread-safe in the very near future). You need to anticipate that using threads might introduce some pernicious bugs that are very difficult to find. Draw on your years of experience and be willing to seek the guidance of those who have been writing multithreaded applications in other languages for years . Remember, even those people will have limited threading experience with .NET, and things that may be true in general may not be true about .NET and threads.
Consider solving problems that can be solved without threads without them, but don't be afraid of or completely avoid using multiple threads. Share your experiences with the general user community, look at the examples in the Microsoft Quickstarts, and check out newsletters. Best practices for threading in .NET will probably evolve over time.
There are basically three kinds of ways you can perform asynchronous tasks in Visual Basic .NET, and you should consider each before committing to any one. You can use a timer or the application idle event for quick-and-dirty lightweight tasks. (You already know how to use timers from VB6.) You can use the thread pool for multithreaded tasks ; the thread pool simplifies thread management significantly. Finally, you can create an instance of the Thread class and manage the threaded task completely. This chapter demonstrates all three variations of asynchronous processing in Visual Basic .NET.