Article

Home » Server-side Coding » ASP & .NET Tutorials » Threading in ASP.NET

About the Author

Joshua Waller

author_joshwaller Josh is currently a Senior at Southeast Missouri State University studying Computer Science. He also works for Element74 Website Designs, where he develops Web applications using ASP.NET.

View all articles by Joshua Waller...

Threading in ASP.NET

By Joshua Waller

January 31st, 2003

Reader Rating: 8

Page: 1 2 3 Next

This tutorial attempts to shed some light on the subject of Threading using ASP.NET. Threading is a technique used to give the user the impression that multiple tasks are executing at the same time. The .NET Framework provides us with many types that allow us to easily and quickly add multi-threading to our .NET Web application. I'll assume that you have some familiarity with ASP.NET and at least some experience with writing code in Visual Basic.

Through the tutorial we'll build a pretty simple Web application that should be a good example of some basic threading concepts. Basically we'll have 2 textboxes in which to enter a phrase, and to enter a time period in which you want the phrase to be rebuilt. Our Web threading application consists of 2 pages, one of which is the page that will start the thread, while the other is a results page that shows us the progress of our thread. The code provided should be very portable and allow you to implement your own threading application very quickly.

Before we get started...

Before we dive into the Web application, let me first give you a quick look at some of the code that you'll be seeing.

First, we need to import the System.Threading Namespace so we can access the Thread class provided in the .NET Framework. Add this line to the top of your .aspx/.ascx file:

<%@ Import NameSpace="System.Threading" %>

or in VB.NET:

Imports System.Threading

Now for demonstration purposes, here is a sample long running method. In a real life situation this method would most likely perform a task like processing a Web form, or completing a time-consuming database query.

Public Sub SomeLongMethod()

       'your code that takes a while to execute

End Sub

Now to execute this method and not leave our Web form hanging, we'll start a new thread and let SomeLongMethod execute on this new thread. To do this, we have a few options. The technique I'll use is to set up a new method that will start our new thread running. Here's a sample thread starter function:

Public Sub SomeLongMethod_Thread()

'first, declare a new Thread, passing the constructor the address
'of SomeLongMethod. NOTE: SomeLongMethod can be replaced with your
'own method

Dim NewThread As Thread = New _
           Thread(AddressOf SomeLongMethod)

'next we set the priority of our thread to lowest, setting
'the priority to a higher level can cause unexpected results.
NewThread.Priority = ThreadPriority.Lowest

'finally we start the thread executing
NewThread.Start()

End Sub

And that's it! All we have to do now is replace our call to SomeLongMethod with a call to SomeLongMethod_Thread, and the long method will execute on its own thread. Normally, we would redirect the user to the results page at the end of the SomeLongMethod_Thread method. However in this example I left that out to prevent confusion -- I'll demonstrate it in the following example, which illustrates the use of Threading in an ASP.NET Web application.

Using Threading to Rebuild a String

The first file we'll look at is default.aspx. This will be the page where we'll get 2 values from the user, and start a new thread. The second file we'll look at is the results page, where we'll poll the Session variables created in the thread, and display current thread stats to our user. I'll go through default.aspx method-by-method, and then do the same for the results.aspx page. The source code for this file is available at the end of this tutorial.

NOTE: I've assumed that your Web server has Session variables enabled. If you have Session variables Disabled, or you have cookies disabled on your browser, the results of the following example will not display correctly.

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

Sponsored Links