In an MVC controller I launch a new task like this:
在MVC控制器中,我启动了一个像这样的新任务:
Task.Run(() => <myFunction>)
It's a task that takes more than half an hour, so I don't want to wait for it: after launching it the program does a few things and sends the view back to the browser.
这是一项需要半个多小时的任务,所以我不想等待它:在启动它之后程序执行一些操作并将视图发送回浏览器。
But the problem is that sometimes the task isn't executed. I think that maybe it is because the main thread stops before the second can be scheduled for execution. Can this be the real reason?
但问题是有时候任务没有执行。我想也许是因为主线程在第二个可以被安排执行之前就停止了。这可能是真正的原因吗?
How can I do to wait till the new task begins its execution, so that I can then send the view to the client?
如何等待新任务开始执行,以便我可以将视图发送给客户端?
1 个解决方案
#1
0
It's a task that takes more than half an hour,
这项任务需要半个多小时,
2 mistakes here. Well, 1 at least. Choose.
这里有2个错误。嗯,至少1。选择。
- If it is a long running task, MARK IT AS SUCH. There is a hint (Tasks.LongRunning) that you can set in the factory.
- Use a thread.
如果这是一项长期运行的任务,请将其标记为此类。您可以在工厂中设置提示(Tasks.LongRunning)。
使用线程。
Do not misdirect the task system. Just in general.
不要误导任务系统。总的来说。
Now...
But the problem is that sometimes the task isn't executed. I think that maybe it is because the main thread stops before the second can be scheduled for execution. Can this be the real reason
但问题是有时候任务没有执行。我想也许是因为主线程在第二个可以被安排执行之前就停止了。这可能是真正的原因
No. WHen Run returns, it returns and the task is scheduled. When it starts runing is left to the task scheduler - although that should be fast unless there is a large queue waiting in front. Want a determined start, either write your own scheduler or use a thread.
没有.When Run返回,它返回并安排任务。当它开始运行时,剩下的就是任务调度程序 - 尽管这应该很快,除非前面有一个大的队列在等待。想要一个坚定的开始,要么编写自己的调度程序,要么使用线程。
What is likely is that the whole asp.net process stops because it is not a container for long running tasks. If you want to schedule a significant time operation then use a container outside of asp.net to run your task - windows services, are made for exactly this. As are the windows task scheduler. Heck, with the right permissions you can just make the task an .exe and start it as a process. I would go with a windows service, though.
可能的是整个asp.net进程停止,因为它不是长时间运行任务的容器。如果你想安排一个重要的时间操作,那么使用asp.net之外的容器来运行你的任务 - 窗口服务就是这样做的。和Windows任务调度程序一样。哎呀,拥有正确的权限,您可以将任务作为一个.exe并将其作为一个进程启动。不过,我会选择Windows服务。
ASP.NET may at any time decide to recycle the whols appdomain / application pool running a website and restart it. For example when no request has been processed for a certain time (I think standard setup ois 20 minutes). Your task will then just be - stopped.
ASP.NET可以随时决定回收运行网站的whols appdomain / application pool并重新启动它。例如,当一段时间内没有处理任何请求时(我认为标准设置为20分钟)。然后你的任务就会被停止。
How can I do to wait till the new task begins its execution,
我该怎么做才能等到新任务开始执行,
- Put in a ManualResetEvent that the main thread waits for.
- Hand it into the task function.
- First thing the task dues is signal it so the main thread continues.
放入主线程等待的ManualResetEvent。
把它交给任务函数。
任务的第一件事就是发出信号,主线继续。
Does not bring anything though because it is fixing the wrong issue. A scheduled task DOES start - at one point when there is time. You just created with this a hanging asp.net worker thread which is a limited ressource to not fix an issue you think is there because of a bad analysis.
不带任何东西,因为它正在解决错误的问题。计划任务开始 - 有时间的某个时刻。你刚刚创建了一个挂起的asp.net工作线程,这是一个有限的资源,无法修复你认为存在的问题,因为分析不好。
#1
0
It's a task that takes more than half an hour,
这项任务需要半个多小时,
2 mistakes here. Well, 1 at least. Choose.
这里有2个错误。嗯,至少1。选择。
- If it is a long running task, MARK IT AS SUCH. There is a hint (Tasks.LongRunning) that you can set in the factory.
- Use a thread.
如果这是一项长期运行的任务,请将其标记为此类。您可以在工厂中设置提示(Tasks.LongRunning)。
使用线程。
Do not misdirect the task system. Just in general.
不要误导任务系统。总的来说。
Now...
But the problem is that sometimes the task isn't executed. I think that maybe it is because the main thread stops before the second can be scheduled for execution. Can this be the real reason
但问题是有时候任务没有执行。我想也许是因为主线程在第二个可以被安排执行之前就停止了。这可能是真正的原因
No. WHen Run returns, it returns and the task is scheduled. When it starts runing is left to the task scheduler - although that should be fast unless there is a large queue waiting in front. Want a determined start, either write your own scheduler or use a thread.
没有.When Run返回,它返回并安排任务。当它开始运行时,剩下的就是任务调度程序 - 尽管这应该很快,除非前面有一个大的队列在等待。想要一个坚定的开始,要么编写自己的调度程序,要么使用线程。
What is likely is that the whole asp.net process stops because it is not a container for long running tasks. If you want to schedule a significant time operation then use a container outside of asp.net to run your task - windows services, are made for exactly this. As are the windows task scheduler. Heck, with the right permissions you can just make the task an .exe and start it as a process. I would go with a windows service, though.
可能的是整个asp.net进程停止,因为它不是长时间运行任务的容器。如果你想安排一个重要的时间操作,那么使用asp.net之外的容器来运行你的任务 - 窗口服务就是这样做的。和Windows任务调度程序一样。哎呀,拥有正确的权限,您可以将任务作为一个.exe并将其作为一个进程启动。不过,我会选择Windows服务。
ASP.NET may at any time decide to recycle the whols appdomain / application pool running a website and restart it. For example when no request has been processed for a certain time (I think standard setup ois 20 minutes). Your task will then just be - stopped.
ASP.NET可以随时决定回收运行网站的whols appdomain / application pool并重新启动它。例如,当一段时间内没有处理任何请求时(我认为标准设置为20分钟)。然后你的任务就会被停止。
How can I do to wait till the new task begins its execution,
我该怎么做才能等到新任务开始执行,
- Put in a ManualResetEvent that the main thread waits for.
- Hand it into the task function.
- First thing the task dues is signal it so the main thread continues.
放入主线程等待的ManualResetEvent。
把它交给任务函数。
任务的第一件事就是发出信号,主线继续。
Does not bring anything though because it is fixing the wrong issue. A scheduled task DOES start - at one point when there is time. You just created with this a hanging asp.net worker thread which is a limited ressource to not fix an issue you think is there because of a bad analysis.
不带任何东西,因为它正在解决错误的问题。计划任务开始 - 有时间的某个时刻。你刚刚创建了一个挂起的asp.net工作线程,这是一个有限的资源,无法修复你认为存在的问题,因为分析不好。