Narendran Murugan
Narendran Murugan

Reputation: 21

Asp.net -- Multithreading in C#

I have a huge data to run which takes awful amount of time so thought Threading might do the job for me quickly.

What I do : Call SQL Stored Procedures from ASP.NET front end and processing takes place there and it takes almost 30 hours.

What I need : I have split the data into different batches and created respective SPs for each. Now I require all SPs to be running at the same time at a single button click.

Please help!

I used the below code but it doesnt seem to run in parallel.

 protected void Button3_Click(object sender, EventArgs e)
    {
        Thread t1 = new Thread(Method1);
        Thread t2 = new Thread(Method2);

        t1.Start();
        t2.Start();

        t1.Join();
        t2.Join();

    }
   void Method1()
    {
        for (int i = 0; i < 10000; i++)
        {
            Response.Write("hello1"+i);
            Response.Write("<br>");
        }
    }

   void Method2()
   {
       for (int i = 0; i < 10000; i++)
       {
           Response.Write("hello2" + i);

       }
   }

Upvotes: 2

Views: 815

Answers (3)

S Nash
S Nash

Reputation: 2499

Narendran, start here:

http://www.albahari.com/threading/

This is the best Threading tutorial I have seen online and respective book is also very good.

Make sure you spend enough time to go through the whole tutorial(I have done it and believe me, it worth it!).

As said above using Join method of thread class in this case defeats the purpose of using threads. Instead of using join use lock(see basic Synchoronization in the above tutorial) to make sure threads are synchronized.

Also as mentioned, before doing multithreading Run those stored procedures on SQL server directly and all together. If it still takes 30 hours for them to get executed ,then using Threading won't do any help. If you see less than 30 hours then you may benefeat from multithreading.

Upvotes: 1

adt
adt

Reputation: 4360

  • Threading doesnt magically speed up your process.
  • If you dont know what you are doing server side threading is not a good idea in general.
  • Sql server probably will time out for 30hrs :)

for 30 hours of job Asp.net is not the way to go. This is a big process and you shouldn't handle it within Asp.net. As an alternative you might want to write a windows service. Pass your parameters to it ( maybe with msmq or some kind of messaging system) Do your process and send progress to web application show it with signalR or ajax pulls.

Upvotes: 1

Lloyd
Lloyd

Reputation: 29668

You probably don't want to be doing this directly in ASP.NET for a variety of reasons, such as the worker process has limited execution time.

Also note that the SqlConnection etc also have their own time limits.

What you should really do is queue up the work to do (using IPC or another database table etc) and have something like a Windows service or external process in a scheduled task pick up and process through the queue.

Hell, you could even kick off a job within SQL Server and have that directly do the work.

Upvotes: 5

Related Questions