gpt4 book ai didi

c# - 如何在网站的进程结束时显示消息?

转载 作者:行者123 更新时间:2023-11-30 16:19:33 24 4
gpt4 key购买 nike

我想在单击网页(asp.net 站点)上的开始按钮时启动一个进程,现在我想将标签文本设置为进程已启动。我想在流程结束时将标签文本设置为“流程已完成”。如何在 asp.net 和 C# 中执行此操作。

提前致谢。

最佳答案

您可能要考虑使用 ASP.NET SignalR .以下是其作用的摘要:

ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

以下是一个简单的网页示例,其中包含一个启动 Notepad.exe 的按钮。进程启动后,页面上的标签会显示 process started。当进程退出时(Notepad 关闭),标签更新为process exited

因此,首先创建一个 ASP.NET 空 Web 应用程序项目(我们将其命名为 MyWebApplication)并获取 Microsoft ASP.NET SignalR NuGet package .将 Web 表单添加到项目并将其命名为 Test。将以下代码添加到 Test.aspx 文件中:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="Test.aspx.cs" Inherits="MyWebApplication.Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"
type="text/javascript"></script>
<script src="Scripts/jquery.signalR-1.0.1.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message) {
$('#label').text(message);
};
// Start the connection
$.connection.hub.start();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" />
<div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button runat="server" Text="Start Notepad.exe"
ID="button" OnClick="button_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="button" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<span id="label"></span>
</div>
</form>
</body>
</html>

向您的项目添加一个新的类文件并将其命名为Chat。在 Chat.cs 中,您将拥有:

using Microsoft.AspNet.SignalR;

namespace MyWebApplication
{
public class Chat : Hub
{
public void Send(string message)
{
//Call the addMessage method on all clients
var c = GlobalHost.ConnectionManager.GetHubContext("Chat");
c.Clients.All.addMessage(message);
}
}
}

将以下内容添加到 Test.aspx.cs 文件:

using System;
using System.Diagnostics;
using Microsoft.AspNet.SignalR;

namespace MyWebApplication
{
public partial class Test : System.Web.UI.Page
{
Chat chat = new Chat();

protected void Page_Load(object sender, EventArgs e)
{
}

void MyProcess_Exited(object sender, EventArgs e)
{
chat.Send("process exited");
}

protected void button_Click(object sender, EventArgs e)
{
Process MyProcess = new Process();
MyProcess.StartInfo = new ProcessStartInfo("notepad.exe");
MyProcess.EnableRaisingEvents = true;
MyProcess.Exited += MyProcess_Exited;
MyProcess.Start();
chat.Send("process started");
}
}
}

添加Global.asax 文件:

using System;
using System.Web.Routing;

namespace MyWebApplication
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
}
}

一些我没有涉及的事情:

  1. 标签在所有连接上更新。
  2. 我不会验证进程是否已经在运行(但这应该不难检查)。

关于c# - 如何在网站的进程结束时显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15222807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com