![]() ![]() | ||
To create a new thread, you use the Thread class's constructor, passing that constructor the address of the Sub procedure that holds the thread's code, like this:
Dim NewThread As New System.Threading.Thread(AddressOf ThreadCodeProcedure)
We saw how this worked in the Threading example on the CD-ROM, as discussed in the In Depth section of this chapter (and you can see this example in Figure 24.10). There, we used a class named counter that had a procedure named Count we used as a thread procedure; this procedure counted from 1 to a value held in the class's CountTo data member and then raised a FinishedCounting event:
Public Class counter Public CountTo As Integer Public Event FinishedCounting(ByVal NumberOfMatches As Integer) Sub Count() Dim LoopIndex, Total As Integer Total = 0 For LoopIndex = 1 To CountTo Total += 1 Next LoopIndex RaiseEvent FinishedCounting(Total) End Sub End Class
Then we created an object of the counter class and a new thread, Thread1, to run the code in the Count procedure:
Dim counter1 As New counter() Dim Thread1 As New System.Threading.Thread(AddressOf counter1.Count)
And that creates the new thread. To see how to work with this thread, examine the next few topics. You can see the Threading example at work in Figure 24.10; here's the code for that example, Form1.vb:
Public Class Form1 Inherits System.Windows.Forms.Form Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Dim counter1 As New counter() Friend WithEvents Button5 As System.Windows.Forms.Button Friend WithEvents Button6 As System.Windows.Forms.Button Dim Thread1 As New System.Threading.Thread(AddressOf counter1.Count) ' Windows Form Designer generated code… Sub FinishedCountingEventHandler(ByVal Count As Integer) TextBox2.Text = Count End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click TextBox2.Text = "" counter1.CountTo = TextBox1.Text AddHandler counter1.FinishedCounting, AddressOf _ FinishedCountingEventHandler Thread1.Start() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Thread1.Abort() End Sub Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click Thread1.Sleep(10 * 1000) End Sub Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click Thread1.Priority = System.Threading.ThreadPriority.BelowNormal End Sub Private Sub Button5_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button5.Click Thread1.Suspend() End Sub Private Sub Button6_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button6.Click Thread1.Resume() End Sub End Class
For more on how this example works, take a look at the discussion in the In Depth section of this chapter.
![]() ![]() | ||