如何调用函数但不等待完成 - ASP.NET

Nov*_*ato 2 c# asp.net cron amazon-s3

我有一个页面,我做了一些文件操作,当文件完成后,我需要上传到亚马逊s3.有时文件可能很大,因此提交的用户需要等待太多.我怎么能做出类似的东西

  1. 文件操作
  2. 完成后,我将文件名参数发送到某个功能
  3. 我不需要等待该功能,我想在上传完成之前使用Response.Redirect.

And*_*day 9

最简单的方法:

ThreadPool.QueueUserWorkItem(YourUploadMethod);
Run Code Online (Sandbox Code Playgroud)

下面有一些评论与此争论,所以我写了这个:

    protected void Page_Load(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(YourUploadMethod);

        Response.Redirect("http://google.com");
    }

    public void YourUploadMethod(object state)
    {
        Thread.Sleep(7000);
    }// breakpoint: I was redirected to google and then debugger stopped me here
Run Code Online (Sandbox Code Playgroud)

  • 我认为Response.End,Response.Redirect或Server.Transfer只会中止主线程.看到这个问题:http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful (3认同)
  • 我不认为这种方法适用于asp.net,因为一旦主线程完成,后台线程也会死掉(无论它们是否完成). (2认同)