简体   繁体   English

从MVC运行短期后台任务

[英]Running a short-lived background task from MVC

I am coding an ASP.NET MVC 3 app. 我正在编写一个ASP.NET MVC 3应用程序。 When a user logs in I need to check a remote system and get the latest data for that user from the system. 当用户登录时,我需要检查远程系统并从系统获取该用户的最新数据。 This task will take approx 15 seconds. 此任务大约需要15秒。

The user should be able to enter my app straight after their login (not have to wait for 15s for the remote call!). 用户应该能够在登录后直接输入我的应用程序(无需等待15秒进行远程呼叫!)。 When the remote call completes the users local information will be updated. 远程呼叫完成后,将更新用户本地信息。

I was thinking of using a thread to do this, creating it after they have logged in and letting it run its course. 我正在考虑使用一个线程来做这件事,在他们登录并让它运行之后创建它。 However, after reading around, I am concerned about recycling etc when working with threads in MVC. 然而,在阅读之后,我在使用MVC中的线程时担心回收等问题。 I would use an async controller, but I dont need to feedback to the user the state of this background process. 我会使用异步控制器,但我不需要向用户反馈此后台进程的状态。 Am I right to be concerned about threads, even if they are short-lived? 我是否正确关注线程,即使它们是短暂的?

" ...concerned about recycling... " ......关注回收...

" ...don't need to feedback to the user the state... " ......不需要向用户反馈国家......

" ...short-lived... " ......昙花一现......

3 reasons why you should be using ThreadPool.QueueUserWorkItem . 您应该使用ThreadPool.QueueUserWorkItem的 3个原因。

Do not use "threads" in an web app. 不要在Web应用程序中使用“线程”。 Let the server handle this by using "async" calls. 让服务器使用“异步”调用来处理这个问题。 Otherwise you would have to setup a threadpool and que the slow request. 否则,您必须设置线程池并对慢速请求进行排队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM