Why ASP.NET kills my background thread?

后端 未结 4 1501
日久生厌
日久生厌 2020-12-14 11:21

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

protected void button1_Click(object sender, EventArgs e)
{
    new Thread(delegate() {
        try
             


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 12:02

    ASP.NET WebForms

    Providing another solution: you don't need to set your page with the async property. The following code will work fine:

    new Thread
    (
        delegate()
        {
            try
            {
                MyMethod(myVar);
            }
            catch (Exception ex)
            {
                // handle
            }
        }
    )
    {
        IsBackground = true
    }.Start();
    

    In this code, IsBackground = true is what prevents the thread to be aborted when the request finishes.

提交回复
热议问题