Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 停止服务后终止进程_C#_Service_Process_Kill - Fatal编程技术网

C# 停止服务后终止进程

C# 停止服务后终止进程,c#,service,process,kill,C#,Service,Process,Kill,我试图在停止服务后终止一个服务进程,服务运行得非常慢-大约7秒,程序根据进程id终止进程,但它得到ArgumentException当我尝试使用命令行执行此操作时,它仍然显示进程正在运行,尽管我需要使用/F(force)终止它 我错过了什么 public void KillService(uint processID) { try { process = Process.GetProcessById

我试图在停止服务后终止一个服务进程,服务运行得非常慢-大约7秒,程序根据进程id终止进程,但它得到ArgumentException当我尝试使用命令行执行此操作时,它仍然显示进程正在运行,尽管我需要使用/F(force)终止它 我错过了什么

    public void KillService(uint processID)
    {
     try
        {                
            process = Process.GetProcessById((int)processID);
            if (process != null)
            {
                process.Kill(); 

            }                                                                
        }
        catch (ArgumentException)
        {
            // Thrown if the process specified by processID
            // is no longer running.               
        }
        catch (Win32Exception)
        {
            // Thrown if process is already terminating,
            // the process is a Win16 exe or the process
            // could not be terminated.

        }
        catch (InvalidOperationException)
        {
            // Thrown if the process has already terminated.

        }
   }

您是否尝试过杀死
进程树
,即进程及其子进程?我找到了这个@Hassan谢谢你的回复,我已经找到了,反正这只是一个进程……你可以检查
进程。首先退出
以防止任何异常。@Nick Thansk,尽管我想捕获异常以用于日志目的:-)