一段多线程的例子:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//make sure the user has entered proper data
'//by relying on the Page.IsValid method which will
'//return false if the user hasn't entered the proper data
If (Page.IsValid = True) Then
'//create a new thread and point it at the
'//generate prime numbers method
Dim NewThread As Thread = New Thread(AddressOf GeneratePrimeNumbers)
'//set the thread priority to something
'//that is acceptable, warning setting the priority
'//to normal or higher will potentially lock up your server,
'//or make your asp.net pages respond very slowly. The best
'//priority to use is lowest
NewThread.Priority = ThreadPriority.Lowest
'//start the new thread running
NewThread.Start()
'//redirect the user to the response page
'//while the thread is working
Response.Redirect("/multithreaded/results1.aspx")
End If
End Sub
Public Sub GeneratePrimeNumbers()
'//create an XmlDocument that we can store our results in
'//and we'll save this information to a session variable when
'//we are done
Dim Results As XmlDocument = New XmlDocument()
Dim Node1 As XmlNode = Results.CreateElement("PrimeNumbers")
Results.AppendChild(Node1)
'//lock the session variable collection so that it
'//thread safety is assurred
SyncLock Session.SyncRoot
Session("Results") = Results.InnerXml
End SyncLock
'//create counter that we use to look through
'//as we move from min to max value looking for
'//prime numbers
Dim Counter As Integer
'//create a for-next looping control structure.
'//we go through each number from min to max looking to see
'//if it is a prime number
For Counter = Convert.ToInt32(MinValue.Text) To Convert.ToInt32(MaxValue.Text) Step 1
'//create a boolean variable and set it to true
'//if we find that the number isn't prime, then we set this to false
Dim prime As Boolean = True
'//create second count. We'll go through and divide counter
'//by each of these numbers. If there isn't a remainder, then
'//the number isn't prime.
Dim Counter2 As Integer
'//This is the second control structure. we start at 2 and move up to the
'//value of counter. During each interation, we divide Counter by Counter2
'//if the operation doesn't return a remainder, then the number isn't prime.
For Counter2 = 2 To (Counter - 1) Step 1
'//here's where we do the dividing. If no remainder, then we simply
'//set the value of the prime variable to false
If (Counter Mod Counter2 = 0) Then
prime = False
End If
Next
If prime = True Then
'//add our prime number to our xml document
'//so that the number can be returned
Dim node2 As XmlNode = Results.CreateElement("Number")
node2.InnerText = Counter
Results.DocumentElement.AppendChild(node2)
'//show update the results session variable
'//provided that the user has selected to show
'//partial results
If (ShowPartial.Checked = True) Then
'//again, lock the session object so that
'//thread safety is assurred
SyncLock Session.SyncRoot
Session("Results") = Results.InnerXml
End SyncLock
End If
End If
Next
'//update the session variable with the final results
'//locking the object first
SyncLock Session.SyncRoot
Session("Results") = Results.InnerXml
'//set the session finished variable to true
'//which indicates that the second thread has completed it's work
Session("Finished") = True
End SyncLock