You might want to consider using ASP.NET SignalR. Here's a summary of what it does:
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.
The following is an example of simple web page with a button which starts Notepad.exe
. Once the process is started, a label on the page shows process started
. When the process exits (Notepad
is closed), the label's updates to process exited
.
So, first create an ASP.NET empty web application project (let's name it MyWebApplication) and get the Microsoft ASP.NET SignalR NuGet package. Add a web form to the project and name it Test. Add the following code to the Test.aspx file:
<%@ 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>
Add a new class file to your project and name it Chat
. In Chat.cs you will have:
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);
}
}
}
Add the following to the Test.aspx.cs file:
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");
}
}
}
Add the Global.asax file:
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();
}
}
}
Some things I haven't covered:
- The label is updated on all connections.
- I'm not verifying if the process is already running or not (but that shouldn't be very difficult to check).