(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: October 2002
Section: Boost Web Performance With Multithreading
Author: Timothy M. Chester

ASP.NET Create the Web Form Code
Listing A  This is the HTML code for the default.aspx Web form.  This page consists of an assortment of ASP.NET controls that generate the interface provided in Figure 1 .

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="default.aspx.vb"
Inherits="multithreaded.DefaultPage"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 
	Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
	<meta name="GENERATOR" 
content="Microsoft Visual Studio.NET 7.0">
	<meta name="CODE_LANGUAGE" content=
		"Visual Basic 7.0">
	<meta name="vs_defaultClientScript" 
		content="JavaScript">
	<meta name="vs_targetSchema"
		content="http://schemas.microsoft.com/
		intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
	<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" 
style="Z-INDEX: 101; LEFT: 16px; POSITION: 
	absolute; TOP: 16px" 
runat="server" Width="440px" Height="32px" 
Font-Names="Verdana" Font-Size="16pt" 
Font-Bold="True">
Generate Some Prime Numbers
	</asp:Label>
	
<asp:RequiredFieldValidator 
id="RequiredFieldValidator2" 
style="Z-INDEX: 111; LEFT: 320px; POSITION: 
	absolute; TOP: 88px" runat="server" 
	Font-Bold="True" Font-Size="10pt" 
Font-Names="Verdana" Height="16px" Width="8px" 
	ControlToValidate="MaxValue" 
	ErrorMessage="*"/>

<asp:RegularExpressionValidator 
	id="RegularExpressionValidator2" style=
	"Z-INDEX: 108; LEFT: 336px; POSITION: 
	absolute; TOP: 88px" runat="server" 
	Width="344px" Height="16px" 
	Font-Names="Verdana" Font-Size="10pt" 
	Font-Bold="True" 
ErrorMessage="* All Digits Please! (1000000 - 
	9999999)" ValidationExpression="[1-
	9]\d\d\d\d\d\d" ControlToValidate="MaxValue" 
	Display="Dynamic"/>
	
<asp:TextBox id="MaxValue" 
style="Z-INDEX: 105; LEFT: 200px; POSITION: 
	absolute; TOP: 88px" runat="server" 
	Width="112px" MaxLength="7" 
	tabIndex="2"></asp:TextBox>

<asp:Label id="Label3" 
style="Z-INDEX: 103; LEFT: 32px; POSITION: 
	absolute; TOP: 88px" runat="server" 
	Width="152px" Height="24px" 
	Font-Names="Verdana" Font-Size="10pt" 
	Font-Bold="True">Maximum Value</asp:Label>

<asp:Label id="Label2" 
style="Z-INDEX: 102; LEFT: 32px; POSITION: 
	absolute; TOP: 56px" runat="server" 
	Width="152px" Height="24px" 
	Font-Names="Verdana" Font-Size="10pt" 
	Font-Bold="True">Minimum Value</asp:Label>
	
<asp:TextBox id="MinValue" 
style="Z-INDEX: 104; LEFT: 200px; POSITION: 
	absolute; TOP: 56px" runat="server" 
	Width="112px" MaxLength="7" tabIndex="1"/>

	<asp:Button id="Button1" 
style="Z-INDEX: 106; LEFT: 16px; POSITION: 
	absolute; TOP: 152px" runat="server" 
	Width="353px" Height="32px" 
	Font-Names="Verdana" Font-Bold="True" 
	Text="Generate Prime Numbers" tabIndex="3"/>

<asp:RegularExpressionValidator 
	id="RegularExpressionValidator1" style=
	"Z-INDEX: 107; LEFT: 336px; POSITION: 
	absolute; TOP: 56px" runat="server" 
	Width="304px" Height="16px" 
	Font-Names="Verdana" Font-Size="10pt" 
	Font-Bold="True" 
ErrorMessage="* All Digits Please! (1000000 - 
	9999999)" ValidationExpression="[1-
	9]\d\d\d\d\d\d" ControlToValidate="MinValue" 
	Display="Dynamic"/>

<asp:CheckBox id="ShowPartial" 
style="Z-INDEX: 109; LEFT: 32px; POSITION: 
	absolute; TOP: 120px" runat="server" 
	Font-Bold="True" Font-Size="10pt" 
	Font-Names="Verdana" Height="24px" 
	Width="376px" 
Text="Show partial results as they are generated" 
	Checked="True"/>

<asp:RequiredFieldValidator 
	id="RequiredFieldValidator1" style=
	"Z-INDEX: 110; LEFT: 320px; POSITION: 
	absolute; TOP: 56px" runat="server" 
	Font-Bold="True" Font-Size="10pt" 
	Font-Names="Verdana" Height="16px" Width="8px" 
	ControlToValidate="MinValue" 
	ErrorMessage="*"/>
		</form>
	</body>
</HTML>

VB.NET	Generate Prime Numbers
Listing 1  This code contains a collection of looping structures that allow the generation of prime numbers.  This VB.NET sub-procedure generates prime numbers within a given range that users enter into the default.aspx Web form. 

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 its thread safety is assured
	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 its work
			Session("Finished") = True
		End SyncLock


	End Sub

VB.NET	Create a New Thread Object
Listing 2  This code creates the Generate Prime Numbers button contained on the default.aspx Web form.  The Button1_Click event creates a new Thread object and assigns it to the GeneratePrimeNumbers() procedure that you saw in Listing 1.

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
