Kill a process in RDC session
-
I launch application B from application A. When i close application A i need to close application B. I RDC to a remote server and launch application A and launch B from A. When i close A it looks for B and kills it. However it goes and kills application B in another RDC session. How do i differentiate the process launched in the session i logged into? Any help would be greatly appreciated.
-
I launch application B from application A. When i close application A i need to close application B. I RDC to a remote server and launch application A and launch B from A. When i close A it looks for B and kills it. However it goes and kills application B in another RDC session. How do i differentiate the process launched in the session i logged into? Any help would be greatly appreciated.
Well, there are quite a few ways to launch a process... In this case, why not start it using the System.Diagnostics.Process class, and just keep a reference to that object?
private Process appB;
public void LaunchAppB()
{
appB = new Process();
appB.StartInfo.FileName = "...";
appB.Start();
}public void KillAppB()
{
if (!appB.CloseMainWindow()) appB.Kill();
}(Note: You generally want to try CloseMainWindow first, which will try to close the app in the usual manner, like clicking the X. If that doesn't work, you kill the process. Depending on exactly what you're doing here, you might need to make it more sophisticated, perhaps giving it a few seconds to close properly and killing after that, or something of the sort)
-
Well, there are quite a few ways to launch a process... In this case, why not start it using the System.Diagnostics.Process class, and just keep a reference to that object?
private Process appB;
public void LaunchAppB()
{
appB = new Process();
appB.StartInfo.FileName = "...";
appB.Start();
}public void KillAppB()
{
if (!appB.CloseMainWindow()) appB.Kill();
}(Note: You generally want to try CloseMainWindow first, which will try to close the app in the usual manner, like clicking the X. If that doesn't work, you kill the process. Depending on exactly what you're doing here, you might need to make it more sophisticated, perhaps giving it a few seconds to close properly and killing after that, or something of the sort)
The context in which i close the application i cannot maintain the instance. Thanks for the response. I found a solution. Check below: [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern IntPtr FindWindow(string sClassName, string sWindowTitle); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); uint processId; IntPtr hProgram = WebApp.FindWindow(null, "....."); GetWindowThreadProcessId(hProgram , out processId); System.Diagnostics.Process.GetProcessById((int)processId).Kill();
-
The context in which i close the application i cannot maintain the instance. Thanks for the response. I found a solution. Check below: [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern IntPtr FindWindow(string sClassName, string sWindowTitle); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); uint processId; IntPtr hProgram = WebApp.FindWindow(null, "....."); GetWindowThreadProcessId(hProgram , out processId); System.Diagnostics.Process.GetProcessById((int)processId).Kill();
I might suggest there is a problem with GetWindowThreadProcessId, I do UI automation testing, i was looking a using GetWindowThreadProcessId a few weeks ago. Bottom line from what i found if you are on Win95, or NT 3.51, GetWindowThreadProcessId should work fine, on Vista I thought i remember seeing warning about it not working correctly. Alternatively, i might suggest 1.) Make sure you app is modal, that only one instance of it can be running. 2.) Get the process by GetProcessesByName() for your process, and then issue a kill on the process you find, well if it is still running. HTH, dan d;)