如何正确执行在ASP.net MVC异步方法? [英] How do I properly execute an Async Method in ASP.net MVC?

查看:636
本文介绍了如何正确执行在ASP.net MVC异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一个控制器方法内执行异步方法和返回的HTTPStatus codeResult(200),没有异步委托prematurely终止其执行?

我工作的一个asp.net应用和我的行动之一,我家的控制器需要很长时间来运行(10-30秒)。我想返回的HTTPStatus codeResult(200),并继续运行我的功能。该functoin不需要返回任何东西,但它仍然不是真正火的情况下,忘记了,因为我返回到服务器的响应马上与的HTTPStatus codeResult

I am working on an asp.net application and one of my action my home controller takes long time to run(10-30 sec). I want to return an HttpStatusCodeResult(200) and keep running my function. The functoin need not to return anything, but still it's not really a case of fire and forget since I return a response to the server right away with HttpStatusCodeResult.

我试着用代表,但似乎一旦我从动作返回状态code,委托停止执行。另一种选择是创建一个Windows服务,但会像使用火箭筒杀死一只苍蝇。我得到很少的请求,因此资源和性能是不是在我的情况的问题。我听说 ThreadPool.QueueUserWorkItem 是一个选项,但是这是最适合我的情况?

I tried using delegates, but it seems once I return the status code from the action, the delegate stops executing. Another option is to create a windows service but that would be like using a bazooka to kill a fly. I get very few requests so resources and performance is not an issue in my case. I heard ThreadPool.QueueUserWorkItem is an option, but which is most suitable for my case?

推荐答案

创建管理这个长期运行操作的静态类。这个类将是负责创建的线程来执行任务,并且还提供一种方法来执行的任何操作的状态清理(即,如果它仍然在进行中或已经完成,如果是的话,则结果是什么)。

Create a static class that manages this long-running operation. This class will be responsible for creating threads to perform the task and also provides a way to check up on the status of any ongoing operations (i.e. if it's still in progress or has finished, and if so, what the result is).

您MVC控制器方法,那么使用这个类做的工作和present状态信息或已完成处理的数据给用户。

Your MVC Controller method then uses this class to do the work and present status information or the completed processed data to the user.

事情是这样的:

public ActionResult GetSomething(String id) {
    TaskResult result = TaskClass.GetStatus( id );
    if( result == null ) { // the task has not been run, so start it up
        TaskClass.StartNew( id );
        return new View("PleaseWait");
    } else if( !result.IsFinished ) {
        return new View("PleaseWait");
    } else {
        return View("Results", result);
    }
}

public static class TaskClass {
     private static Dictionary<String,TaskResult> _tasks;
     public static TaskResult GetStatus(String id) {
         // TODO: Make this code thread-safe
         if( _tasks.ContainsKey(id) ) return _tasks[id];
         return null;
     }
     public static void Start(String id) {
         _tasks.Add( id, new TaskResult("Working") );
         Thread thread = new Thread( SomeExpensiveOperation );
         thread.Start( id );
     }
}

目前SomeExpensiveOperation年底会有code作为完成标志着该TaskResult。

At the end of SomeExpensiveOperation there would be code that marks the TaskResult as finished.

这篇关于如何正确执行在ASP.net MVC异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆