1

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

Can i programmatically call that X button by PID?

enter image description here

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

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

3 Answers3

3

This is part of my code using system command 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)
    {

    }
}
Adam Jachocki
  • 1,897
  • 1
  • 12
  • 28
0

You need find window and send message to the window

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

like have described here

Community
  • 1
  • 1
Aleksandr Zolotov
  • 1,078
  • 3
  • 19
  • 32
0

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();
Elis
  • 91
  • 1
  • 4
  • Have you checked if your process has any childs that are preventing it from closing? The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit. like described in the [link](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow(v=vs.110).aspx) – Elis May 22 '17 at 08:30
  • 1
    CloseMainWindow only works when a process has window. This process does not have a window. It starts in command line. – Adam Jachocki May 22 '17 at 09:44