Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Threading Issue in a web application

I have a web application and in the code behind page, I have a button click event.

protected void btnUpload_Click(object sender, EventArgs e)
        {
            TimerEvent TE = new TimerEvent();
            TE.TimerEvents();
            Server.Transfer("~/Jobs.aspx");
        }

I am having a method call TimerEvents() and it may take 10-15 seconds to get the control back to the server transfer. so I want the server transfer line to be executed simultaneously at the same time of the method call.

I tried threading as in here:

    TimerEvent TE = new TimerEvent();
    Thread t = new Thread(TE.TimerEvents);
    t.IsBackground = true;
    t.Start();           
    Server.Transfer("~/Jobs.aspx");

But the transfer is not made, until the method is finished. How can I achieve this?

like image 697
superstar Avatar asked Jan 24 '11 02:01

superstar


1 Answers

This design is not ideal. Even if you get it right, there's a chance you'll run into problems down the road due to the mixing of multi-threading code and the Server.Transfer() call.

There are also issues regarding multi-threading in ASP.NET applications.

I suggest you re-engineer your app so that you have some kind of queue that queues up long-running tasks. You can then then have a service that processes tasks in the queue one by one.

From your web application, you'll now be able to:

  1. Queue up the long running TimerEvents task to the external service.

  2. Call Server.Transfer()

  3. On the new page ("~/Jobs.aspx"), call the external service to pick up your results when you need it.

like image 103
tenor Avatar answered Sep 24 '22 14:09

tenor