简体   繁体   English

可以使用按钮来运行cpu密集型代码,然后使用c#和asp.net为Web应用程序显示结果吗?

[英]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: 因此,该架构的基本概述如下所示:

  1. A user in the web application clicks a button to "start the task." Web应用程序中的用户单击按钮以“启动任务”。
  2. The web application inserts a record into a database table indicating that the task has been queued (maybe with a user ID of who queued it, a time stamp, anything else you'll need to know). 该Web应用程序将一条记录插入数据库表中,以表明该任务已排队(也许带有排队的任务的用户ID,时间戳,您需要了解的其他信息)。
  3. A separate scheduled application (console application or Windows Service, most likely) is perpetually running. 永久运行单独的计划应用程序(控制台应用程序或Windows Service)。 (Either using a timer in an always-running Windows Service or scheduled to run over and over, such as every few minutes, as a console application.) This application checks the database table for new queued tasks. (可以在始终运行的Windows Service中使用计时器,也可以计划将其作为控制台应用程序反复运行(例如每隔几分钟)。)此应用程序检查数据库表中是否有新的排队任务。
  4. When the application sees a task, it marks it as "started" in the database (so subsequent runs of the application don't try to run the same task in parallel) and starts running it. 当应用程序看到任务时,它将在数据库中将其标记为“已启动”(因此,应用程序的后续运行不会尝试并行运行同一任务)并开始运行它。
  5. The web application can see the status of the task in the database table and display it to users who request it, so users can see that it's still running. 该Web应用程序可以在数据库表中查看该任务的状态,并将其显示给请求该任务的用户,因此用户可以看到该任务仍在运行。
  6. When the task is completed, the task record in the database table is updated and the result is stored somewhere. 任务完成后,数据库表中的任务记录将更新,并将结果存储在某处。 (Depending on what the result is. Data? In the database. A report file of some sort? Save as a file somewhere. That's all up to you.) (取决于结果是什么。数据?在数据库中。某种报表文件?另存为文件。这完全取决于您。)
  7. The web application can see the status of the task as completed and any other information recorded, and users can request to view the output of the task. Web应用程序可以查看已完成任务的状态以及记录的任何其他信息,并且用户可以请求查看任务的输出。

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.

相关问题 C# ASP.NET Core - 在已部署的 Web 应用程序/API 中定期/自动运行代码 - C# ASP.NET Core - run code periodically/automatically in a deployed web app/API 如何在ASP.Net Web表单中使用Roslyn执行C#代码? - How I can execute C# code using Roslyn in ASP.Net web form? 如何使用C#获取运行时在ASP.NET Web应用程序中的按钮单击事件上创建的文本框的值 - How to get value of textboxes created at run time on button click event in asp.net web application using c# 在asp.net C#Web窗体上将按钮悬停时显示字符串变量 - Display string variable when button hover on asp.net C# web form ASP.NET C#:如何在 web.config 中调用 App_code 函数 - ASP.NET C#: How can I call App_code functions inside of web.config 可以在asp.net c#中将web.config键值对用作编译时常量吗? - Can a web.config key value pair be used as a compile time constant in asp.net c#? 带有生成的HTML代码的Web应用程序包含不显示消息的复选框。 (Asp.net,C#) - Web application with generated HTML code consist of checkbox not display message. (Asp.net, C#) ASP.NET web 形式:显示值来自 c# 代码隐藏 - ASP.NET web form: display value coming from c# code-behind 在C#ASP.NET应用程序中自动单击“网页”按钮 - Automate clicking on Web Page button in C# ASP.NET app 单击时,asp.net c#命令按钮不运行命令代码 - asp.net c# Command button doesn't run command code when clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM