|
I recently needed to set up a page that could be called
to kill a particular process on the server that had an annoying tendency
to get "hung" at various and sundry times. Although it really
didn't interfere with anything particular if it did, I guess I'm just
one of those people who can't stand to have extra unwanted "baggage"
hanging around in memory. At first I was ready to look for WMI and classic
ASP as we've already done some of this stuff.
But then it occured to me that since we have ASP.NET
on the server, why not let's take a look and see what base classes are
available and maybe I can sort of "kill two birds with one stone"
- solve my problem, and learn something new about the .NET platform, all
at the same time.
It turns out that System.Diagnostics is uniquely
suited to handle stuff like this. In fact it was so simple to do, that
the page you see below only took me barely 10 minutes to write:
<% @Page Language="C#"%>
<% @ Import namespace= "System.Diagnostics" %>
<html>
<head>
<script language="C#" runat="Server">
void Page_Load(Object Sender, EventArgs e){
// add custom onclick attribute
here...
btnKill.Attributes.Add("onclick", "javascript: return confirm('Are
you sure you want to KILL this process?');");
if(Page.IsPostBack)
{
KillProcess(procname.Text);
}
}
private void KillProcess(string
processName){
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//Get all instances of proc that are open, attempt to close them.
try{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if(!thisproc.CloseMainWindow()){
//If closing is not successful or no desktop window handle, then force
termination.
thisproc.Kill();
}
} // next proc
}
catch(Exception Exc)
{
msg.Text+= "Attempt to kill " +procname.Text + " Failed.
";
}
}
// add custom btnKill_Click
handler here ...
public void btnKill_Click(object sender, System.EventArgs e)
{
msg.Text="Killed " + procname.Text +".";
}
</script>
</head>
<body>
<center><h2>ASP.NET PROCESS KILLER!</h2><BR>
<form id="frmProc" runat="Server" method="POST">
<asp:TextBox id="procname" runat="server" /><BR>Process
Name to Kill<BR>
<asp:button id="btnKill" Text="Kill Process" runat="server"
CausesValidation="False" onclick="btnKill_Click" /><BR>
<center><asp:Label id="msg" runat="server"/></center>
</form>
</CENTER>
</body>
</html>
Note that I've
taken this opportunity to show how to wire up a client-side "confirm"
behavior when the KILL button is pressed. This is just a nice extra "sanity
check" for the user to make sure they REALLY want to kill the specified
process. And of course, I know I don't need to really say this, but I
will anyway: You ONLY want to put a page like this in an IIS directory
that is specifically marked for Administrator authenticated access! When
you enter a process name, you leave off the filename extension.
For example, if you want to kill a running instance of wscript.exe, you
would enter "wscript". Enjoy.
Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer
/Analyst at in Orlando and a co-developer of the EggheadCafe.com
developer website. He can be reached at pbromberg@yahoo.com
|