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.
Now that you have seen a kind of simple graphical representation of what threads are and how we can manipulate them and make our programs run more efficiently; Let's actually look at a program that spawns multiple threads, and let me show you what the results are. Let's go out to visual studio and I've written a real simple little thread example here, and let me show you what happens. You are going to see a form with three buttons on it, and button one when we click it, will simply create a message box on the primary thread of an application just like always. And when we do this it will be the only thread running and it will be the primary and we will not be able to do anything else until we satisfy the program, the message box on that thread. Now button two if I click it first notice what's happening, I am Dimming W as WorkerClass, and setting that equal to New Worker class. Well, worker class is a class that I've created up here and if you'll notice it has a function, a sub called DoSomeWork and its going to create a message box that tells us first background thread and why its going to say that? Because to create on this New worker class object W, I am going to Dim backgroundThread as a new instance of a thread, and notice we use a delegate here, we talked about those a couple of videos back, and we are going to tell it that this new thread runs on the address of this W object that I've created and the DoSomeWork method there. And then I will tell the thread to start, and so the thread will start, it will go up here and on this object run this DoSomeWork method on a new thread, on a background thread. There will be a third button on the form that'll do the same thing and kick off another thread; this one will instantiate another object off of WorkerClass2 up here and you'll notice WorkerClass2, and it has a message box, it has a function. I mean a subroutine - DoSomeMoreWork - and it produces a message box called second background thread. And just like before we'll - Dim thirdThread As New Thread - a delegate address of y for the object - y.DoSomeMoreWork - that's the method we are going to call. And then we'll start that third thread and so you'll see as I run this how these multiple threads affect how the program runs. So as this comes up, I am going to minimize visual studio so here is our form. Now notice if I click on the primary thread, this kicks the program off and I can't move the form around, because I've got to satisfy the message box that's running on this primary thread. But if I kick off my background thread first, notice I get a thread here and this is running on a background thread and my form here is running on the primary thread. So I can move this around. I can move them both around; they are independent of one another. If I kick off another background thread, notice I can still move everybody around because these three sections of the program are running on different threads. Now I can kick off more threads, OK, and take care of this now. Notice if I satisfy all these, everything is cool. If I go back to primary again I am running just on a single thread; so that's what an application looks like. Now a good example of these, a good real world example again is Microsoft office; and the spell checker runs on a separate thread in the background and when it sees us type a bad word, a misspelt word, it puts a squiggly line under it and tells us that we've done so. So that's what threads look like, that's how we spawn them, that's how they behave. So very powerful feature, and in the next video we are going to talk about some issues that you need to be concerned with as you use threads in Visual Basic.
| Course: | Microsoft Visual Basic .NET |
| Author: | Mark Long |
| SKU: | 33433 |
| ISBN: | 1932072349 |
| Release Date: | 2003-05-27 |
| Duration: | 6 hrs / 87 lessons |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |