private void RunProcessAsync() { RunProcessDelegate process = new RunProcessDelegate(RunProcess); AsyncCallback callback = delegate { this._stopProcess = false; if (OnProcessFinished != null) { OnProcessFinished(); } if (this._mustRestart) { this._mustRestart = false; this.Start(); } }; process.BeginInvoke(callback, null); if (OnProcessStarted != null) { OnProcessStarted(); } }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { var processName = ProcessName?.Get(context); if (string.IsNullOrEmpty(processName)) { throw new ArgumentException("No process name was given."); } var inputValues = InputValues?.Get(context); var runProcess = new RunProcessDelegate(RunProcess); context.UserState = runProcess; return(runProcess.BeginInvoke(processName, inputValues, callback, state)); }
public static string RunConsoleApp(string strAppPath, string strParams, string strStdInput, AppRunFlags f) { if (strAppPath == null) { throw new ArgumentNullException("strAppPath"); } if (strAppPath.Length == 0) { throw new ArgumentException("strAppPath"); } bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None); RunProcessDelegate fnRun = delegate() { try { ProcessStartInfo psi = new ProcessStartInfo(); psi.CreateNoWindow = true; psi.FileName = strAppPath; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.RedirectStandardOutput = bStdOut; if (strStdInput != null) { psi.RedirectStandardInput = true; } if (!string.IsNullOrEmpty(strParams)) { psi.Arguments = strParams; } Process p = Process.Start(psi); if (strStdInput != null) { // Workaround for Mono Process StdIn BOM bug; // https://sourceforge.net/p/keepass/bugs/1219/ EnsureNoBom(p.StandardInput); p.StandardInput.Write(strStdInput); p.StandardInput.Close(); } string strOutput = string.Empty; if (bStdOut) { strOutput = p.StandardOutput.ReadToEnd(); } if ((f & AppRunFlags.WaitForExit) != AppRunFlags.None) { p.WaitForExit(); } else if ((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None) { Thread th = new Thread(delegate() { try { p.WaitForExit(); } catch (Exception) { Debug.Assert(false); } }); th.Start(); } return(strOutput); } catch (Exception) { Debug.Assert(false); } return(null); }; if ((f & AppRunFlags.DoEvents) != AppRunFlags.None) { List <Form> lDisabledForms = new List <Form>(); if ((f & AppRunFlags.DisableForms) != AppRunFlags.None) { foreach (Form form in Application.OpenForms) { if (!form.Enabled) { continue; } lDisabledForms.Add(form); form.Enabled = false; } } IAsyncResult ar = fnRun.BeginInvoke(null, null); while (!ar.AsyncWaitHandle.WaitOne(0)) { Application.DoEvents(); Thread.Sleep(2); } string strRet = fnRun.EndInvoke(ar); for (int i = lDisabledForms.Count - 1; i >= 0; --i) { lDisabledForms[i].Enabled = true; } return(strRet); } return(fnRun()); }
public static string RunConsoleApp(string strAppPath, string strParams, string strStdInput, AppRunFlags f) { if (strAppPath == null) { throw new ArgumentNullException("strAppPath"); } if (strAppPath.Length == 0) { throw new ArgumentException("strAppPath"); } bool bStdOut = ((f & AppRunFlags.GetStdOutput) != AppRunFlags.None); RunProcessDelegate fnRun = delegate() { Process pToDispose = null; try { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = strAppPath; if (!string.IsNullOrEmpty(strParams)) { psi.Arguments = strParams; } psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.RedirectStandardOutput = bStdOut; if (strStdInput != null) { psi.RedirectStandardInput = true; } Process p = StartProcessEx(psi); pToDispose = p; if (strStdInput != null) { EnsureNoBom(p.StandardInput); p.StandardInput.Write(strStdInput); p.StandardInput.Close(); } string strOutput = string.Empty; if (bStdOut) { strOutput = p.StandardOutput.ReadToEnd(); } if ((f & AppRunFlags.WaitForExit) != AppRunFlags.None) { p.WaitForExit(); } else if ((f & AppRunFlags.GCKeepAlive) != AppRunFlags.None) { pToDispose = null; // Thread disposes it Thread th = new Thread(delegate() { try { p.WaitForExit(); p.Dispose(); } catch (Exception) { Debug.Assert(false); } }); th.Start(); } return(strOutput); } #if DEBUG catch (ThreadAbortException) { } catch (Win32Exception exW) { Debug.Assert((strAppPath == ClipboardU.XSel) && (exW.NativeErrorCode == 2)); // XSel not found } catch (Exception) { Debug.Assert(false); } #else catch (Exception) { } #endif finally { try { if (pToDispose != null) { pToDispose.Dispose(); } } catch (Exception) { Debug.Assert(false); } } return(null); }; if ((f & AppRunFlags.DoEvents) != AppRunFlags.None) { List <Form> lDisabledForms = new List <Form>(); if ((f & AppRunFlags.DisableForms) != AppRunFlags.None) { foreach (Form form in Application.OpenForms) { if (!form.Enabled) { continue; } lDisabledForms.Add(form); form.Enabled = false; } } IAsyncResult ar = fnRun.BeginInvoke(null, null); while (!ar.AsyncWaitHandle.WaitOne(0)) { Application.DoEvents(); Thread.Sleep(2); } string strRet = fnRun.EndInvoke(ar); for (int i = lDisabledForms.Count - 1; i >= 0; --i) { lDisabledForms[i].Enabled = true; } return(strRet); } return(fnRun()); }