private void Run() { try { string exeFile = m_ExeFileProp.Value; if (string.IsNullOrEmpty(exeFile)) { Lib.ShowError("Please select executable file."); m_ExeFileProp.MyComboBox.Focus(); return; } string workingDir = m_WorkingDirProp.Value; if( string.IsNullOrEmpty( workingDir ) ) workingDir = Path.GetDirectoryName( exeFile ); string args = m_ArgsMemoProp.Value; if( !string.IsNullOrEmpty( args ) ) { args = args.Replace( '\r', ' ' ); args = args.Replace( '\n', ' ' ); } m_ExeFileProp.PostValueToMRU(); m_WorkingDirProp.PostValueToMRU(); Lib.WaitCursor = true; m_ProcessOutput = new ProcessOutput(); m_LoadingImageIndex = 0; m_PictureBox.Image = Lib.GetLoadingImage(m_LoadingImageIndex); m_Process = new Process(); m_Process.StartInfo.FileName = exeFile; if (args != null) m_Process.StartInfo.Arguments = args; if (workingDir != null) m_Process.StartInfo.WorkingDirectory = workingDir; m_Process.StartInfo.RedirectStandardInput = true; m_Process.StartInfo.RedirectStandardOutput = true; m_Process.StartInfo.RedirectStandardError = true; m_Process.StartInfo.UseShellExecute = false; m_Process.StartInfo.CreateNoWindow = true; m_Process.EnableRaisingEvents = true; m_Process.OutputDataReceived += new DataReceivedEventHandler(m_ProcessOutput.OnOutputDataReceived); m_Process.ErrorDataReceived += new DataReceivedEventHandler(m_ProcessOutput.OnErrorDataReceived); m_Process.Exited += new EventHandler(m_ProcessOutput.OnProcessExited); m_Process.Start(); m_Process.BeginOutputReadLine(); m_Process.BeginErrorReadLine(); m_UpdateTimer = new Timer(); m_UpdateTimer.Tick += new EventHandler(OnUpdateTimerTick); m_UpdateTimer.Interval = 200; m_UpdateTimer.Start(); ShowOutputPage(); m_KillButton.Visible = true; Lib.PrevButton.Visible = false; } catch( Exception ex ) { m_Process.Close(); m_Process = null; m_ProcessOutput = null; Lib.ShowError("Cannot start process.", ex); } finally { Lib.WaitCursor = false; } }
private void OnUpdateTimerTick(object sender, EventArgs e) { if (m_Process == null) return; if (m_ProcessOutput.AppendOutputToRichTextBox(m_OutputControl.MyRichTextBox)) { m_LoadingImageIndex = (m_LoadingImageIndex + 1) % 8; m_PictureBox.Image = Lib.GetLoadingImage(m_LoadingImageIndex); } if (m_ProcessOutput.Exited) { int exitCode = m_Process.ExitCode; m_InfoTextBox.Value = string.Format( "Result: {0} (0x{1:X8})\r\n" + "Duration: {2}", exitCode, (uint)exitCode, m_Process.ExitTime - m_Process.StartTime); m_PictureBox.Image = exitCode == 0 ? NoConsoleLib.Properties.Resources.circle_green : NoConsoleLib.Properties.Resources.circle_red; m_KillButton.Visible = false; Lib.PrevButton.Visible = true; m_Process.Close(); m_Process = null; m_ProcessOutput = null; m_UpdateTimer.Stop(); m_UpdateTimer = null; } // Process is still working. else { m_InfoTextBox.Value = string.Format( "Duration: {0}", DateTime.Now - m_Process.StartTime); } }