Home > DeveloperSection > Forums > ASP.NET kills my background thread?
Ankit Singh

Total Post:341

Points:2389
Posted on    November-17-2014 11:16 PM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 529  View(s)
Rate this:

I have the following code in my codebehind (aspx.cs):

protected void button1_Click(object sender, EventArgs e)

{

    new Thread(delegate() {

        try

        {

            Thread.Sleep(30000); //do nothing for 30 seconds

        }

        catch (Exception ex)

        {

            //I AWLAYS get a ThreadAbortException here

            //in about 1 second WHY!?!??!

        }

    }).Start();

}


Please help me



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    November-17-2014 11:41 PM

Hi Ankit, 
try this :

protected void button1_Click(object sender, EventArgs e)

{

    var bg = new BackgroundWorker();

    bg.DoWork += delegate

    {

        try

        {

            Thread.Sleep(10000); //do nothing for 10 seconds

        }

        catch (Exception ex)

        {

            //no excpeiton is thrown

        }

    };

    bg.RunWorkerAsync();

}

Hope this will solve your problem


Don't want to miss updates? Please click the below button!

Follow MindStick