Home
Username:
Password:
Microsoft Visual Basic .NET Tutorials

Threads / Threading Issues




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Ok so we finally got threading in Visual Basic and a new day has dawned. But let me warn you before you jump on threading too hard, that there are some advantages to threading and there are some disadvantages to threading. Now, there are some times when threading is an excellent idea and by all means you should employ it. There are some other times when threading will really shoot you in the foot, and you really want to be careful about these things. First of all let's look at the advantages; threading most definitely will give you more responsive applications. Because while the user's doing something in the foreground, something else can be going on in the background. Example is Excel, the way it calculates spreadsheets; calculation takes place in the background. And so now, before you have a Visual Basic out running, you can be taking care of some business in the background while the user continues to input or make choices on the foreground and that's nice. You can also set priorities with threads. So you can have one thread running at a higher priority than another thread, and so we can get these foreground and background kind of issues going, and that's really neat for performance. Another great use for threads is networking issues, making the database connections, passing things to databases, handling resources over networks in the background and while the user is continuing to work in the foreground. And users really like this because they just perceive that the application is running a lot faster and a lot more smoothly. Now you do need to be aware of some serious disadvantages that threading can bring about. First of all is a thing called context switches. Now think about this, if we've got three threads running on a single processor, what the processor's doing is time slicing. Its running one thread for a little while, then it is stopping running the second thread then stopping, running the third thread. Now every time it moves from the one thread to the other it has to address a different set of memory locations and so forth, and it does what is called a context switch. Now these things are fairly resource intensive, and if we get too many threads going and too many context switches going, then the time slice gets eaten up by the resource drain on these, and the thread actually doesn't run very long and doesn't make much progress. And so if it looks as if the entire application is slowing down, then actually it is. We are actually doing more work on the processor switching contexts then we are with our application and so that becomes a problem. Another issue you need to be concerned with threading is when you have shared access to resources. If I have two threads accessing the same variable, I've got to make sure that I stay in sync with the process, or one which would change the variable before other one is ready or before one has taken all the actions it needed to with that variable. So again you'll just have to keep an eye out for these issues. Now management can become an issue. If you spawn too many threads, you can introduce them in your program for a number of different reasons because the different threads are doing different things to shared resources at different places in your program. So there are some threading issues you need to be aware of, I would strongly suggest you spend some time out in the SDK, the software development kit documentation. Read up on threads, get used to them, and by all means try them: they are fantastic, employ them where you can but be careful with them and don't go wild. These are not a silver bullet and they can slow you down as much as they can help you. You just need to have a good well-rounded understanding of all the things they offer you, and at the same time what they cost.

Tutorial Information

Course: Microsoft Visual Basic .NET
Author: Mark Long
SKU: 33433
ISBN: 1932072349
Release Date: 2003-05-27
Duration: 6 hrs / 87 lessons
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available