如何在网站的进程端显示消息

本文关键字:显示 消息 进程 网站 | 更新日期: 2023-09-27 18:02:33

当我点击网页(asp.net站点)上的开始按钮时,我想启动一个进程,现在我想将标签文本设置为进程启动。并且我想在过程结束时将标签文本设置为"Process Completed"。如何在asp.net和c#中做到这一点。

如何在网站的进程端显示消息

您可能需要考虑使用ASP。净SignalR。以下是它的功能摘要:

ASP。. NET SignalR是一个面向asp.net的新库。NET开发人员令人难以置信的简单添加实时web功能到您的应用程序。什么是"实时网络"功能?这是能够让服务器端代码将内容推送到连接的服务器.

下面是一个简单的网页的例子,有一个按钮开始于Notepad.exe。进程启动后,页面上的标签显示为process started。当进程退出(Notepad关闭)时,标签更新为process exited

那么,首先创建一个ASP。. NET空web应用程序项目(让我们将其命名为MyWebApplication)。. NET SignalR NuGet包。向项目添加一个web表单,并将其命名为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");
        }
    }
}

添加全局变量。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. 我没有验证进程是否已经运行(但这应该不是很难检查)。

使用javascript执行回调。在每个舞台上;发起、完成或错误;更新html中的标签。如果您寻找一些使用jQuery AJAX的示例,这应该相当简单。

jQuery AJAX POST示例

如果你不想使用javascript…你所能做的就是在触发button click事件时首先更改标签文本。

lblLabel.text="process started"

和button_click事件的最后一行应该像:

lblLable.text="process completed";

添加到CodeBehind:

ScriptManager.RegisterStartupScript(this, GetType(), "Records Inserted Successfuly", "Showalert();", true);
在源代码(aspx)中添加:
 function Showalert() {
            alert('Records inserted Successfully!');
        }

并添加使用System.Web.UI;

你可以简单地像这样在aspx..中添加一个标签到webform .

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

和在asp .cs后面的代码中添加.

Labelname.Text = "whatever msg you wanna display."