Chapter 14: Debugging Multithreaded Applications


"Multithreading" is a word that strikes fear into the hearts of many VB.Classic developers. Although VB 5.0 introduced a restricted type of multithreading called apartment threading, VB .NET is the first version of Visual Basic to have proper free threading. This means that you've been given a very powerful tool to make your application faster and more responsive to its users. As with any powerful technology, it requires that you have a good understanding of the benefits and drawbacks in order to use it properly and safely.

When I started skydiving many years ago, I never forgot the sign above the clubhouse door. It read simply "Knowledge dispels fear." In no area of software development is this saying more true than multithreading. Most Visual Basic developers are relatively unfamiliar with the subject, and even those programmers who tried VB.Classic development using apartment threading will be worried when faced with free threading. The key to avoiding threading bugs lies in a good knowledge of what can go wrong and why it goes wrong.

This chapter looks at how and why multithreading is so difficult, and gives you some knowledge and tools that will help you to tackle this subject safely. It starts with a quick look at how multithreading works and why it's so difficult to do correctly. Then it examines when multithreading is, and isn't, useful. Knowing when not to use multithreading is important because it can save you a lot of time in testing and debugging.

The next section of this chapter looks in more detail at the sorts of bugs that can be introduced when writing multithreaded programs. It demonstrates why it's necessary to design your multithreaded applications to avoid bugs rather than trying to remove the bugs later. The elusive nature of many multithreading bugs means that the normal code test debug cycle doesn't work well when writing multithreaded programs.

I'll show you the four main types of threading problems in some detail, using small example applications to illustrate the issues and some possible solutions. The final example application shows you how to use multithreading safely in a typical graphical user interface (GUI) program. It looks at debugging a relatively simple multithreaded application that uses messages to pass information between a user interface thread and a background thread, thus avoiding thread synchronization problems. You'll also learn how to propagate an exception across thread boundaries, even when using asynchronous threads.

Comprehensive VB .NET Debugging
Comprehensive VB .NET Debugging
ISBN: 1590590503
EAN: 2147483647
Year: 2003
Pages: 160
Authors: Mark Pearce © 2008-2017.
If you may any questions please contact us: