简体   繁体   English

如何通过PID向CMD进程发送关闭命令

[英]How can i send close command to a CMD process by PID

This process is started by selenium 这个过程由硒开始

So i do not have control over it how i start it 所以我无法控制它是如何开始的

However, the CMD process have X button 但是,CMD进程有X按钮

Can i programmatically call that X button by PID? 我可以通过PID以编程方式调用该X按钮吗?

在此输入图像描述

C# 4.6.2 windows 8.1 C#4.6.2 windows 8.1

I am able to kill the process however it is not making the same effect as X button click 我能够杀死该过程,但它没有与X按钮点击产生相同的效果

This is part of my code using system command taskkill: 这是使用系统命令taskkill的代码的一部分:

//starts a command
public static int RunCommandAndWait(string command, string args)
{
    int result = -1;

    Process p = new Process();
    p.StartInfo.Arguments = args;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.FileName = command;
    p.StartInfo.UseShellExecute = true;

    if (p.Start())
    {
        p.WaitForExit();
        result = p.ExitCode;
    }

    p.Dispose();
    return result;
}

//kills a process using its pid and above method
public static void KillProcessTree(int processId)
{
    RunCommandAndWait("taskkill", String.Format("/pid {0} /f /t", processId));
    try
    {
        Process p = Process.GetProcessById(processId);
        if (p != null)
            KillProcessTree(processId); //this sometimes may be needed
    }catch(Exception)
    {

    }
}

You need find window and send message to the window 您需要找到窗口并向窗口 发送消息

SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

like have described here 就像这里描述的那样

This should do the trick: 这应该做的伎俩:

// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();

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

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