简体   繁体   English

无法关闭C#中的进程

[英]Can't close down a process in C#

I am running an executable that opens a windows form from a webform. 我正在运行从Web窗体打开Windows窗体的可执行文件。 In visual studio the winform program runs a method and then closes the windows form correctly and shuts down the program. 在Visual Studio中,winform程序运行一个方法,然后正确关闭Windows窗体并关闭该程序。 But when I run the same file as an executable it keeps the windows form open. 但是,当我运行与可执行文件相同的文件时,它将使Windows窗体保持打开状态。 I can see that this executable process is still running as SmartSheetAPI.exe. 我可以看到该可执行进程仍作为SmartSheetAPI.exe运行。

在此处输入图片说明

When I check in properties the name of the file is "SmartSheetAPI.exe". 当我签入属性时,文件的名称为“ SmartSheetAPI.exe”。 If I end this process in the task manager it shuts down the windows form so I know that is the issue. 如果我在任务管理器中结束此过程,它将关闭Windows窗体,因此我知道这是问题所在。 However, I have tried using the below code on the webform to kill the process but again it doesn't work. 但是,我尝试在网络表单上使用以下代码杀死该进程,但再次无法正常工作。

Process process = new Process();
process.StartInfo.FileName = @"P:\Visual Studio 2013\Projects\Smartsheet\SmartsheetAPI\obj\Debug\SmartSheetAPI.exe";
process.Start();
foreach (var processes in Process.GetProcessesByName("SmartSheetAPI.exe"))
{
    process.Kill();
}

Does anyone know how to shut this thing down. 有谁知道如何关闭这个东西。 As I say it works well in the SmartSheetAPI program in visual studio but doesn't shutdown the window as an executable. 就像我说的那样,它在Visual Studio的SmartSheetAPI程序中效果很好,但不会以可执行文件的身份关闭窗口。 I just need to shut down this process once it has run the method. 一旦运行该方法,我只需要关闭此过程即可。

EDIT: The process that isn't closing is the vshost.exe and as such it is keep my application from closing for some reason (ie the windows form remains open). 编辑:没有关闭的过程是vshost.exe,因此它使我的应用程序由于某种原因而关闭(即Windows窗体保持打开状态)。 If I process.kill() this everything shuts down as required. 如果我process.kill()这一切都将按要求关闭。 However, the problem is that when I run the executable of that file the windows form stays open but I can't find the vshost.exe running to close it? 但是,问题是,当我运行该文件的可执行文件时,Windows窗体保持打开状态,但找不到运行中的vshost.exe将其关闭吗? I have disabled it and now the process that won't close is the SmartSheetAPI.exe file which is the program I am currently running. 我已禁用它,现在无法关闭的进程是SmartSheetAPI.exe文件,该文件是我当前正在运行的程序。 I just want to exit out of this program, but nothing I try seems to work. 我只想退出该程序,但我尝试执行的任何操作似乎都无效。

After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited. 调用Kill方法之后,请调用WaitForExit方法以等待进程退出,或者检查HasExited属性以确定进程是否退出。

Process process = new Process();
process.StartInfo.FileName = @"P:\Visual Studio 2013\Projects\Smartsheet\SmartsheetAPI\obj\Debug\SmartSheetAPI.exe";
process.Start();
// do some stuff while process is alive ...
process.Kill();
process.WaitForExit();
// do stuff after the process has been killed ...

For more details see here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(v=vs.110).aspx 有关更多详细信息,请参见此处: http : //msdn.microsoft.com/zh-cn/library/system.diagnostics.process.kill(v=vs.110).aspx

If your process cannot be killed it is probably waiting for something else. 如果您的进程无法终止,则可能正在等待其他事件。 You might want to investigate that rather than forcing your application to be terminated. 您可能希望对此进行调查,而不是强制终止应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM