[英]Can a button be used to run cpu intensive code and then display the results using c# and asp.net for a web app
I am currently trying to create an application which has a homepage. 我目前正在尝试创建一个具有主页的应用程序。 On this homepage there is a button which is going to run CPU intensive code and then display the results. 在此主页上,有一个按钮,它将运行CPU密集型代码,然后显示结果。 As I stated in the title the application is using ASP.NET and C# not Windows Forms which some other websites I have looked at seem to suggest. 正如我在标题中所述,该应用程序使用的是ASP.NET和C#,而不是Windows Forms,我看过的其他一些网站似乎暗示了这一点。
I am relatively new to C# and have not attempted this in any other programming language. 我对C#还是比较陌生,还没有尝试使用其他任何编程语言进行此操作。 I also have no code to show as I don't really know how to progress this. 我也没有要显示的代码,因为我真的不知道该如何进行。
It can run CPU intensive code on the server, yes. 是的,它可以在服务器上运行CPU密集型代码。 Note that this will typically block your web application from sending a response in a reasonable amount of time and the web application could appear to be frozen. 请注意,这通常会阻止您的Web应用程序在合理的时间内发送响应,并且该Web应用程序似乎已冻结。
What type of CPU intensive code did you have in mind? 您打算使用哪种类型的CPU密集型代码? There are many ways to handle this scenario. 有许多方法可以处理这种情况。
By "CPU intensive" I assume you mean it will take a long time for the process to complete? 通过“ CPU密集型”,我想您是说该过程需要很长时间才能完成? In a web application anything that takes more than a few moments should be done asynchronously. 在Web应用程序中,所有花费多于几分钟的时间都应异步完成。 In the request/response model of HTTP it's best (for a number of reasons) to respond quickly to a client making a request. 在HTTP的请求/响应模型中,最好(出于多种原因)最好快速响应发出请求的客户端。
In the case of a long-running process, by "asynchronous" I do not mean using AJAX, as that's still a request/response like any other. 对于长时间运行的进程,“异步”并不是指使用AJAX,因为它仍然像其他任何请求/响应一样。
By "asynchronous" what I mean in this case is that you want to have a separate server-side process which handles the CPU intensive task, and the web application does nothing more than queue the task for running and check the status of the task when people look for it. “异步”在这种情况下的意思是,您希望有一个单独的服务器端进程来处理CPU密集型任务,并且Web应用程序除了将任务排队等待运行并检查任务状态时,无外乎人们在寻找它。 Then it can report the results of the task after it's done. 然后,它可以在完成任务后报告任务的结果。
So a basic overview of the architecture would be something like this: 因此,该架构的基本概述如下所示:
The main thing to remember here is to break up the responsibilities into two applications . 这里要记住的主要事情是将职责分为两个应用程序 。 The web application is for the purpose of providing a user interface. 该Web应用程序旨在提供用户界面。 A web application is not suited for long-running background tasks. Web应用程序不适合长时间运行的后台任务。 So that responsibility is moved to a separate application which is better suited for that purpose. 因此,将责任转移到更适合该目的的单独应用程序中。 The two applications coordinate via a shared database. 这两个应用程序通过共享数据库进行协调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.