public void KillProcessTree(AppProcess process, CommandOptions options, TimeSpan timeout) { if (process == null) { throw new ArgumentNullException(nameof(process)); } string stdout; string stderr; if (_isWindows) { ProcessUtility.KillProcessWindows(process.ProcessId, _fileSystem, out stdout, out stderr); LogOutputs(stdout, stderr, options); } else { var children = new HashSet <int>(); ProcessUtility.GetAllChildIdsUnix(process.ProcessId, children, _fileSystem); foreach (var childId in children) { ProcessUtility.KillProcessUnix(childId, _fileSystem, out stdout, out stderr); LogOutputs(stdout, stderr, options); } ProcessUtility.KillProcessUnix(process.ProcessId, _fileSystem, out stdout, out stderr); LogOutputs(stdout, stderr, options); } process.Kill(timeout, _console); }
public bool EmbedProcess(int width, int height) { OpenExternProcess(width, height); try { var pluginWinHandle = AppProcess.MainWindowHandle; //Get the handle of main window. embedResult = Win32API.SetParent(pluginWinHandle, _hostWinHandle); //set parent window Win32API.SetWindowLong(new HandleRef(this, pluginWinHandle), Win32API.GWL_STYLE, Win32API.WS_VISIBLE); //Set window style to "None". var moveResult = Win32API.MoveWindow(pluginWinHandle, 0, 0, width, height, true); //Move window to fixed position(up-left is (0,0), and low-right is (width, height)). //embed failed, and tries again if (!moveResult || embedResult == 0) { AppProcess.Kill(); if (MAXCOUNT-- > 0) { EmbedProcess(width, height); } } else { Win32API.ShowWindow(pluginWinHandle, (short)Win32API.SW_MAXIMIZE); } } catch (Exception ex) { var errorString = Win32API.GetLastError(); MessageBox.Show(errorString + ex.Message); } return(embedResult != 0); }
public void OnKill_is_called_if_set_when_process_is_killed() { int callCount = 0; AppProcess process = new AppProcess(new Process(), AppTask.Test, AppStatus.Running) { OnKill = (processId) => callCount++ }; process.Kill(); Assert.Equal(1, callCount); }
/// <summary> /// Close <code>AppFilename</code> /// <para>将属性<code>AppFilename</code>指向的应用程序关闭</para> /// </summary> public void Stop() { if (AppProcess != null)// && AppProcess.MainWindowHandle != IntPtr.Zero) { try { if (!AppProcess.HasExited) { AppProcess.Kill(); } } catch (Exception) { } AppProcess = null; embedResult = 0; } }
public void Start() { if (AppProcess != null) { Stop(); } try { if (m_products == null || !File.Exists(m_products.ExePath)) { return; } ProcessStartInfo info = new ProcessStartInfo(m_products.ExePath); info.Verb = "open"; info.UseShellExecute = false; info.WindowStyle = ProcessWindowStyle.Minimized; AppProcess = Process.Start(info); Application.Idle += appIdleEvent; AppProcess.Exited += AppProcess_Exited; AppProcess.EnableRaisingEvents = true; AppProcess.WaitForInputIdle(); } catch (Exception ex) { MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}", Environment.NewLine, "*" + ex.ToString(), "*StackTrace:" + ex.StackTrace, "*Source:" + ex.Source), "Failed to load app."); if (AppProcess != null) { if (!AppProcess.HasExited) { AppProcess.Kill(); } AppProcess = null; } } }
/// <summary> /// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器 /// </summary> public void Start(string Arguments) { if (AppProcess != null) { Stop(); } try { ProcessStartInfo info = new ProcessStartInfo(this.m_AppFilename); info.UseShellExecute = true; info.WindowStyle = ProcessWindowStyle.Minimized; info.Arguments = Arguments; //info.WindowStyle = ProcessWindowStyle.Hidden; AppProcess = System.Diagnostics.Process.Start(info); // Wait for process to be created and enter idle condition AppProcess.WaitForInputIdle(); //todo:下面这两句会引发 NullReferenceException 异常,不知道怎么回事 //AppProcess.Exited += new EventHandler(AppProcess_Exited); //AppProcess.EnableRaisingEvents = true; Application.Idle += appIdleEvent; } catch (Exception ex) { MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}" , Environment.NewLine , "*" + ex.ToString() , "*StackTrace:" + ex.StackTrace , "*Source:" + ex.Source ), "Failed to load app."); if (AppProcess != null) { if (!AppProcess.HasExited) { AppProcess.Kill(); } AppProcess = null; } } }
/// <summary> /// 将属性<code>AppFilename</code>指向的应用程序打开并嵌入此容器 /// </summary> public void Start() { if (AppProcess != null) { Stop(); } try { ProcessStartInfo info = new ProcessStartInfo(this.m_AppFilename); info.UseShellExecute = true; //info.UseShellExecute = false; //info.RedirectStandardOutput = true; //info.WindowStyle = ProcessWindowStyle.Minimized; ZZQ //info.WindowStyle = ProcessWindowStyle.Hidden; info.Arguments = "-gGUIITEMS_96"; //-h1984 info.WorkingDirectory = Path.GetDirectoryName(this.m_AppFilename); //info.Verb = "runas"; AppProcess = System.Diagnostics.Process.Start(info); // Wait for process to be created and enter idle condition AppProcess.WaitForInputIdle(); //todo:下面这两句会引发 NullReferenceException 异常 //AppProcess.Exited += new EventHandler(AppProcess_Exited); //AppProcess.EnableRaisingEvents = true; Application.Idle += appIdleEvent; } catch (Exception ex) { /* * int i = 0; * int len = 100; * IntPtr hwnd; * do * { * Thread.Sleep(2000); * hwnd = Win32API.FindWindow("Qt5QWindowIcon", null); //"V-REP PRO EDU - " * StringBuilder WindowTitle = new StringBuilder(len); * int rlt = Win32API.GetWindowText(hwnd, WindowTitle, len); * if (hwnd != IntPtr.Zero) * { * if (String.Compare("V-REP PRO EDU - ", 0, WindowTitle.ToString(), 0, 15) !=0) * hwnd = IntPtr.Zero; * } * i++; * }while (hwnd == IntPtr.Zero && i < 30); * * if (hwnd == IntPtr.Zero) * { * MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}" * , Environment.NewLine * , "*" + ex.ToString() * , "*StackTrace:" + ex.StackTrace * , "*Source:" + ex.Source * ), "Failed to load app."); * * } * else * { * if (EmbedProcess(AppProcess, this)) * { * Application.Idle -= appIdleEvent; * } * else if (AppProcess != null) * { * if (!AppProcess.HasExited) * AppProcess.Kill(); * AppProcess = null; * } * }*/ } try { int i = 0; int len = 100; IntPtr hwnd; do { Thread.Sleep(3000); hwnd = Win32API.FindWindow("Qt5QWindowIcon", null); //"V-REP PRO EDU - " StringBuilder WindowTitle = new StringBuilder(len); int rlt = Win32API.GetWindowText(hwnd, WindowTitle, len); if (hwnd != IntPtr.Zero) { if (String.Compare("V-REP PLAYER - ", 0, WindowTitle.ToString(), 0, 12) != 0) { hwnd = IntPtr.Zero; } //if (String.Compare("V-REP PRO EDU - ", 0, WindowTitle.ToString(), 0, 13) != 0) //15 // hwnd = IntPtr.Zero; } i++; } while (hwnd == IntPtr.Zero && i < 5); //5 if (hwnd == IntPtr.Zero) { MessageBox.Show(this, string.Format("{1}{0}{2}{0}" , Environment.NewLine , "* Error:" , "*Source file: " + m_AppFilename ), "Failed to load app."); } else { if (EmbedProcess(AppProcess, this)) { Application.Idle -= appIdleEvent; } else if (AppProcess != null) { if (!AppProcess.HasExited) { AppProcess.Kill(); } AppProcess = null; } } } catch (Exception ex) { MessageBox.Show(this, string.Format("{1}{0}{2}{0}{3}" , Environment.NewLine , "*" + ex.ToString() , "*StackTrace:" + ex.StackTrace , "*Source:" + ex.Source ), "Failed to load app."); } }