C# 如何杀死 startet 进程?
我在 ASP.NET 中开发了一个网页 - 该页面有一个按钮,单击即可启动 DOS 程序。
这是代码:
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());
}
代码工作正常并启动进程。
现在的问题:
我单击按钮,进程开始(我在数据库中看到这一点)并且程序运行正常,但是,我现在想停止该进程,但不知道如何。
没有 DOS 窗口,我可以在其中单击“CTRL + C”或其他东西。
我可以做什么来停止该进程?
哪个用户用于启动该进程?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用
Process.Kill
< /a>:更新:
由于您的评论,我提供此作为替代方案:
要在没有代码的情况下终止进程,请使用任务管理器(使用 CTRL + SHIFT + ESC 或 CTRL + ALT + DEL -> 任务管理器打开它),导航到“进程”选项卡,选择“显示所有用户的进程” ,选择您的进程并单击“结束进程”...
Just use
Process.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"...
您可以将 Process 对象存储为页面类下的字段,并使用另一个按钮调用 .Kill at。但在这种情况下,您将只能托管 1 个进程,并且应该想办法确保只运行一个进程是安全的。
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.