Multithreading with ASP.Net 2.0

Every good developer should understand the basics of how Threads and Processes work on the platform they are writing applications for.  For us it’s the Windows operating system and using the .Net Framework it’s even easier to harness the power of Multithreading.

Good Multithreading design can vastly improve your applications user experience and I have a little example below of how Multithreading can be used within an ASP.Net 2.0 website.

Our goal here is to send a request to the server and write out 101 lines to a text file on the webserver.  The WriteOutTextFile() method takes a very long time to complete as I have deliberatly made the method run slow by making the thread go to sleep for 500ms every iteration of the loop.  Now say this method was used as a logging tool behind the scenes, we wouldn’t want our user waiting for the method to write out all 101 lines before the web page was returnd to them. The user doesn’t care that your logging method takes a long time they just want to see their request returned to them, this is where multithreading comes in. We open up a new web site in Visual Studio and we have a simple application as shown below:

I put one label and one button on the form and our markup looks like this:

We then switch to our code behind and write the following code in the OnLoad event:

Here the first thing we are doing is setting the label’s text property to “PostBack!” which is pretty standard stuff, the interesting part is the next four lines.   In version 2.0 of the .Net Framework multithreading was made so much easier by introducing the ThreadStart class which allows us to easily pass the method we want to be called when our threads starts to the Thread class.  The ParameterizedThreadStart class makes things even easier when you want to pass arguments to your methods and anyone who has done multithreading in .Net 1.1 knows how much of a headache this was. So what we are doing in these four lines is creating an instance of the ThreadStart class to tell our new thread to call our worker method WriteOutTextFile when we start out new thread. We then go ahead and create our new Thread and call it’s Start() method. This will kick off our WriteOutTextFile() method on a new thread and allow the OnLoad event to finish execution and return our request back to the user.  Our new thread will continue executing and writing to our text file in the background. The WriteOutTextFile() method looks like this:

That’s a simple example of how multithreading can be used to good effect with the .Net 2.0 framework.  As you can see it’s a very powerful tool and mastering multithreading can make your applications more performant, much more scalable and give your end user and less frustrating experience. I have uploaded the source code here, if you want to download and anaylse it yourself.

kick it on

11 thoughts on “Multithreading with ASP.Net 2.0

  1. Ur, why are you using a parameterized threadstart when your threaded method doesn’t take any parameters?

    Also, this technically isn’t multithreading with ASP.NET. This is multithreading AND ASP.NET. Most people are interested in using multithreading within the response, not as a means to kick off functions that have no bearing on the response (tho that may be useful). I came to this blog post to see some stuff on how you can handle a response asynchronously and then rejoin the request at the completion of the asynchronous operation(s). That shit is hard to understand, which is why I like reading up on it (I may actually use it someday!). This, not so much.

  2. Your right I am using a parameterized ThreatStart when I shouldn’t be, this was because I changed the code to test something and never changed it back, my mistake. My method name is wrong also I have just noticed, I need to update that code sample.

    The post was just to demonstrate how to do multithreading with .Net and I happened to use ASP.Net as my UI, the code could easily be used within a WinForm app too.

    It was meant to be quite high level for people who have never done any multithreading before, sorry it wan’t what you were looking for.

  3. Thanks for the info.
    I’m looking into writing a logging system that logs to a SQL DB and don’t want the user hanging around while this happens 🙂

    Looks like multi-threading it would be a good idea.


  4. Hi!

    I want to make better my SQL capabilities.
    I red that many SQL books and still feel, that I am not an expert
    in SQL. What would you recommend?


  5. Hi,
    I did the same thing in my application but my thread gets aborted as sson as default.aspx get loaded.
    I tried ISBackground = true but still the thread gets aborted as soon as the page load is complete.
    Here is the code:
    private Thread bgThread;
    ThreadStart threadStart = new ThreadStart(myClass.myFunc);
    bgThread = new Thread(threadStart);

  6. I got this error “The name ‘PrintConsoleMessage’ does not exist in the current context”

    Please help me.


  7. i want to download multiple files simultaneously using multithreding.
    hav u any idea then rply soon.
    it is urgent.
    Thanks in Advance.
    Alok kumar

  8. Guy .. Excellent .. Superb .. I will bookmark your site and take the feeds additionallyI’m happy to seek out so many useful info here within the submit, we’d like work out extra techniques on this regard, thank you for sharing. . . . . .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s