Article
Threading in ASP.NET
The other interesting technique we utilize is the use of the Synclock method. Synclock is used to block other threads from trying to obtain the same Synclock. To protect the variables from simultaneous access, we need to obtain the same Synclock before we access the variables everywhere else in the code. This is a necessity in a multi-threaded Web application to ensure that two methods aren't reading/writing to the same variable at the same time. The Synclock method is identical to using the Monitor.Enter(Me) method that's also provided in the System.Threading Namespace.
There are only two methods left to go! The next method we'll look at is the PhraseBuilder_Thread function. This function is almost identical to the example at the beginning of the article:
Sub PhraseBuilder_Thread()
'method to start our phrase builder method executing
'on a new thread.
Dim myThread As Thread = New Thread(AddressOf PhraseBuilder)
myThread.Priority = ThreadPriority.Lowest
'//start the new thread
myThread.Start()
'now redirect to the results page
Response.Redirect("results.aspx")
End Sub
Now all that's left is to call our PhraseBuilder_Thread method when the user clicks the submit button. Here's the short code:
Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs)
'start PhraseBuilder thread...
PhraseBuilder_Thread()
End Sub
A Simple Example(results.aspx)
Now we'll take a look at the results page. Basically our results page will check the status of the Session("completed") variable on the Page_Load, and react accordingly. Here's the Page_load function for results.aspx:
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Put user code to initialize the page here
'check the value of Session("Completed"), if it is True, stop writing
If Session("Complete") <> True Then
'make sure session variables are enabled, if not warn user
If Session("Complete") <> False Then
'error with session variable, Session("Complete") is not
'True or False
lblComplete.Text = "Error with Session('Complete')"
Else
'set page to auto refresh page every 2 seconds, until thread is done
Response.Write("<META HTTP-EQUIV=Refresh CONTENT='2; URL='>")
SyncLock Session.SyncRoot
lblStatus.Text = Session("Status") & "<br>Processing. . ."
lblPhrase.Text = Session("Phrase")
lblComplete.Text = Session("Complete")
End SyncLock
End If
Else
'thread is complete, stop writing refresh tag, and display
'results
SyncLock Session.SyncRoot
lblStatus.Text = Session("Status")
lblPhrase.Text = Session("Phrase")
lblComplete.Text = Session("Complete")
End SyncLock
End If
End Sub
This Page_Load function checks the status of the Session("Complete") variable and reacts accordingly. Feel free to customize you results page to suit you needs, but I recommend checking for the case when the Session("Completed") variable is not set to True, or False. This usually happens when Session variables are disabled, or you have cookies disabled. Also, you can remove the Response.Write statement if you don't want the page to automatically refresh.
In Conclusion
Well that's all there is to it! Hopefully you have just written your first ASP.NET application using threading.
Using threading in the right places in your applications can greatly improve Web server performance, and also provide you users with a much better Web experience. I hope you enjoyed this tutorial! For more information, visit:
- Helpful Threading Tutorials on the Web
- Implementing a Background Thread in Visual Basic.NET
- Boost Web Performance With Multithreading