2

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);

       }
   }
5
  • 4
    ASP.Net pipeline run in a single thread and die once the final rendering is done. Basically, that means that the pages do stay alive on the server, but that pages are instantiated for each request, used to generate the resulting html, then are released. Please read the documentation to understand this model before going on. There are solutions (signalR + out-of-iis application), but you should know how asp.net works in general before. Commented Jun 3, 2013 at 12:55
  • When you call t1.Join, you're telling the current thread to wait until t1 is complete, which is why the code isn't running parallelly Commented Jun 3, 2013 at 12:55
  • Writing 20,000 things to an ASP.NET page? Wouldn't you want to paginate or something? Commented Jun 3, 2013 at 12:56
  • @stuartd: But when i omit the t1.join line it gives the below error Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Commented Jun 3, 2013 at 13:26
  • @MarkSchultheiss: NO i dont want to print 10000lines. Its just a sample attempt to check whether async threading is working Commented Jun 3, 2013 at 13:27

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Lloyd for your reply. Could you please suggest me some blog post or any article for the solution you have given me?.
1
  • 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.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.