Avijit Pramanik's Blog

Avijit Pramanik's Blog

Multithreading Usage in C# and ASP.Net

Why Multithreading in C# and ASP.Net

Usage of multithreading in .Net can be very useful for different use cases. 
 
  1. Suppose in your web page there are multiple report that needs to be generated based on different complex queries, stored procedures or functions. If you execute one query at a time and populate the Report in GridViews, it will take longer time to complete. Instead of this if you execute multiple queries in parallel and populate those GridViews then the maximum time it may take up to the time to execute the query that take longest to finish.
  2. Sometimes in your page there might be scenarios where you want to show one particular set of data. But if user wants then they can click a button (Lets say “Show More”) and then more data can be shown. You can optimize this kind of scenario by getting data in advance for “Show More” button and that you can easily do by using threading and storing the data in the page and retrieve that by Javascript when required.
 
So threading could be a game changer for your website if you can effectively use it.
 
In the below example I have shown a simplest threading implementation where two different methods has been called by Threading or Non-Threading way and we can see that how Threading way of implementation is executing faster.




Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace TestWebApplication
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
            Serial Execution
            */
            writeLn("Without Threading");
            writeLn("==========================");
            DateTime dt1 = DateTime.Now;
            writeLn("Started at " + dt1);
            doWork1();
            doWork2();
            DateTime dt2 = DateTime.Now;
            writeLn("Ended at " + dt2);
            writeLn("--------------------------");
            writeLn("Time Taken: " + (dt2 - dt1).Seconds + " Second");
            writeLn("");
            writeLn("");


            /*
            Parallel execusion by using Threads
            */
            writeLn("With Threading");
            writeLn("==========================");
            DateTime dt3 = DateTime.Now;
            writeLn("Started at " + dt3);
            Thread t1 = new Thread(new ThreadStart(doWork1));
            t1.Start();

            Thread t2 = new Thread(new ThreadStart(doWork2));
            t2.Start();

            t1.Join();
            t2.Join();
            DateTime dt4 = DateTime.Now;
            writeLn("Ended at " + dt4);
            writeLn("--------------------------");
            writeLn("Time Taken: " + (dt4- dt3).Seconds + " Second");
        }

        private void doWork1()
        {
            writeLn("First Method Started");
            Thread.Sleep(1000);
            writeLn("First Method Ended");
        }

        private void doWork2()
        {
            writeLn("Second Method Started");
            Thread.Sleep(1000);
            writeLn("Second Method Ended");
        }

        private void writeLn(String msg)
        {
            Response.Write(msg + "
");
        }
    }
}

Output

Without Threading
==========================
Started at 7/15/2016 12:55:52 AM
First Method Started
First Method Ended
Second Method Started
Second Method Ended
Ended at 7/15/2016 12:55:54 AM
--------------------------
Time Taken: 2 Second


With Threading
==========================
Started at 7/15/2016 12:55:54 AM
First Method Started
Second Method Started
First Method Ended
Second Method Ended
Ended at 7/15/2016 12:55:55 AM
--------------------------
Time Taken: 1 Second

For using Threading more optimize way in .Net you can read about Thread Pool and implement that as and when required. Will definitely write in near future on Thread Pool in .Net.