Article

Threading in ASP.NET

Page: 1 2 3 Next

A Simple Example (default.aspx)

Let's begin by looking at the Page_Load function for default.aspx

Sub Page_Load(ByVal sender As System.Object, ByVal e  
As System.EventArgs)  
 
SyncLock Session.SyncRoot  
'here, we initialize 3 session variables to hold our results  
Session("Complete") = False  
Session("Status") = ""  
Session("Phrase") = ""  
End SyncLock  
d Sub

In this method we simply initialize 3 session variables that we'll use in our next few methods. Session("Complete") will be the sentinel for our results page. When the thread is complete we will set Session("Complete") to True. The Session("Phrase") variable will be what we use to hold our partial phrase as we slowly build it. Session("Status") is just a variable to hold the start time and the end time. Now let's look at our phrase re-building method:

Sub PhraseBuilder()  
 
 
       Dim str As String = ""  
       Dim i As Integer = 0  
 
       Dim startTimeTicks As Long = 0  
       Dim strStartTime As String = ""  
 
       Dim totalSleepTime As Double = 0.0  
 
       'log our start time, in ticks, and in Long Date format  
       startTimeTicks = DateTime.Now.Ticks  
       strStartTime = "Thread Started: " & DateTime.Now  
 
       ' get phrase  
       str = txtPhrase.Text  
 
       'convert users time from seconds to milliseconds  
       totalSleepTime = 1000.0  
       totalSleepTime = totalSleepTime * CInt(txtTotalThreadLife.Text)  
       totalSleepTime = (totalSleepTime / str.Length)  
 
       For i = 0 To str.Length - 1  
 
       'this method will put our thread to sleep for the specified  
       'number of milliseconds. without the sleep, this method would  
       'execute too fast to see the thread working.  
       Thread.Sleep(totalSleepTime)  
 
 'we use synclock to block any other thread from accessing  
 'session variables while we are changing their values.  
       SyncLock Session.SyncRoot  
         
 Session("Status") = "Thread is " & _  
 Format((i / (str.Length - 1)) * 100, "#0.00") & _  
       "% complete." & " - Time Elapsed: " & _  
       Format((DateTime.Now.Ticks - startTimeTicks) / 10000000 _  
       , "#0.00") & " sec. Target: " & txtTotalThreadLife.Text & _  
       ".00 sec."  
 
       End SyncLock  
 
       SyncLock Session.SyncRoot  
 'rebuild phrase 1 letter at a time  
       Session("Phrase") &= str.Chars(i).ToString  
       End SyncLock  
 
 
       Next  
 
 'our method is complete, so set the Session variables  
 'accordingly  
       SyncLock Session.SyncRoot  
       Session("Status") = strStartTime & _  
 "<br>Thread Complete. End Time: " & DateTime.Now & "<br>"  
 
       Session("Complete") = True  
       End SyncLock  
 
   End Sub

Ok, now let's dissect this method a little. Basically what we're doing here is forcing a method that would otherwise run quickly to run on a schedule based on user input. This is done using the Thread.Sleep(ByVal millisecond as Integer) method. This method allows us to put the thread to sleep for the specified number of milliseconds. This Sleep method can be used in any method, not just one that's executing on a new thread.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links