I developed a Webpage in ASP.NET - the page has a button that starts a DOS-Program on click.
Here is the Code:
if (!String.IsNullOrEmpty(endTime))
{
Process p = new Process();
p.StartInfo.WorkingDirectory = Server.MapPath("~/");
p.StartInfo.FileName = Server.MapPath(apiFile);
try
{
p.Start();
}
catch
{
throw new Exception(
"Requested process (" + p.StartInfo.FileName 开发者_开发百科+ ") could not be started. Page: Api.aspx.cs, Method: UploadBtn_Click()");
}
System.Threading.Thread.Sleep(5000);
Response.Redirect(Request.Url.ToString());
}
The code works fine and starts the Process.
Now the Problem:
I click on the button and the process starts (I see this in the database) and the program works fine, however, I want to stop the process now, but don't know how.
There isn't a DOS-Window where I can click "CTRL + C" or something.
What can I do to stop the process?
And which User is used for starting the Process?
Just use Process.Kill
:
p.Kill();
UPDATE:
Because of your comment, I provide this as an alternative:
To kill a process without code, use the task manager (open it by using CTRL + SHIFT + ESC or CTRL + ALT + DEL -> Task manager), navigate to the "Processes" tab, choose "Show processes from all users", select your process and click "End process"...
You can store Process object as field under the page class and call .Kill at with another button. But in this case you will be able to host only 1 process and should think of the way to make it safe to run only one.
精彩评论