如何从 ASP.NET 页面运行冗长的任务?
我有一个带有简单表单的 ASP.NET 页面。用户填写表单并提供一些详细信息,上传文档,然后需要在服务器端对文件进行一些处理。
我的问题是 - 处理文件的服务器端处理的最佳方法是什么?该处理涉及调用 exe。我应该为此使用单独的线程吗?
理想情况下,我希望用户提交表单,而在处理过程中网页不会挂在那里。
我已经尝试过这段代码,但我的任务从未在服务器上运行:
Action<object> action = (object obj) =>
{
// Create a .xdu file for this job
string xduFile = launcher.CreateSingleJobBatchFile(LanguagePair, SourceFileLocation);
// Launch the job
launcher.ProcessJob(xduFile);
};
Task job = new Task(action, "test");
job.Start();
任何建议都表示赞赏。
I've got an ASP.NET page with a simple form. The user fills out the form with some details, uploads a document, and some processing of the file then needs to happens on the server side.
My question is - what's the best approach to handling the server side processing of the files? The processing involves calling an exe. Should I use seperate threads for this?
Ideally I want the user to submit the form without the web page just hanging there while the processing takes place.
I've tried this code but my task never runs on the server:
Action<object> action = (object obj) =>
{
// Create a .xdu file for this job
string xduFile = launcher.CreateSingleJobBatchFile(LanguagePair, SourceFileLocation);
// Launch the job
launcher.ProcessJob(xduFile);
};
Task job = new Task(action, "test");
job.Start();
Any suggestions are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以以经典的即发即忘方式异步调用处理功能:
在 .NET 4.0 中,您应该使用新的 任务并行库:
如果您需要将参数传递给操作委托,您可以这样做:
在 .NET 3.5 及更早版本中,您可以这样做:
相关资源:
You could invoke the processing functionality asynchronously in a classic fire and forget fashion:
In .NET 4.0 you should do this using the new Task Parallel Library:
If you need to pass an argument to the action delegate you could do it like this:
In .NET 3.5 and earlier you would instead do it this way:
Related resources:
用途:
其中MyFunc()在用户提交页面后在后台进行服务器端处理;
Use:
Where MyFunc() does the server-side processing in the background after the user submits the page;
我有一个网站,它执行一些可能需要长时间运行的操作,这些操作需要响应并为用户更新计时器。
我的解决方案是在页面中构建一个带有隐藏值和一些会话值的状态机。
我的 aspx 端有这些:
我的代码看起来像这样:
I have a site that does some potentially long running stuff that needs to be responsive and update a timer for the user.
My solution was to build a state machine into the page with a hidden value and some session values.
I have these on my aspx side:
And my code looks something like: