private void StopProcess() { try { try { SendCMD(config["ExitCMD"].ToString()); } catch (Exception) { p.Kill(); } try { if (p.MainWindowHandle != IntPtr.Zero) { User32API.ShowWindow(p.MainWindowHandle, User32API.SW_SHOWNA); } } catch (Exception) { } //p.StartInfo.RedirectStandardOutput = false; //p.StartInfo.RedirectStandardInput = false; } catch (Exception) { } }
private void StartProcess() { if (p != null) { try { p.Kill(); p.Dispose(); } catch (Exception) { } } p = new Process(); string path = Regex.Replace(config.Value <string>("basicFilePath"), @"^~\\?", string.IsNullOrEmpty(SlnPath) ? "" : (Path.GetDirectoryName(SlnPath) + "\\")); p.StartInfo.FileName = path; p.StartInfo.WorkingDirectory = Path.GetDirectoryName(p.StartInfo.FileName); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardError = true; if (config.ContainsKey("showWindow")) { if (!config.Value <bool>("showWindow")) { #if DEBUG p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; #else p.StartInfo.CreateNoWindow = true; #endif } } p.EnableRaisingEvents = true; var encoding = GetEncoding(config.Value <string>("OutPutEncoding")); p.StartInfo.StandardOutputEncoding = encoding; p.StartInfo.StandardErrorEncoding = encoding; WriteLine("当前编码:" + p.StartInfo.StandardOutputEncoding); p.Start(); p.BeginErrorReadLine(); p.ErrorDataReceived += (_s, _e) => WriteLine(_e.Data); p.BeginOutputReadLine(); p.OutputDataReceived += (_s, _e) => OnConsoleReadLine(_e.Data); p.Exited += (_s, _e) => { WriteLine("---Main Process Exited---"); }; try { Task.Run(() => { for (int i = 0; i < 2000; i++) { Thread.Sleep(10); if (p.MainWindowHandle != IntPtr.Zero) { User32API.ShowWindow(p.MainWindowHandle, User32API.SW_HIDE); break; } if (p.HasExited) { break; } } //WriteLine(p.HandleCount); //WriteLine(p.MainWindowHandle); User32API.ShowWindow(p.Handle, User32API.SW_MINIMIZE); //User32API.ShowWindow(User32API.GetPWindowHandle(p.Id), User32API.SW_MINIMIZE); }); } catch (Exception) { } }