Execute the executable file and display the result on the UI in WinForm.
Note:
The source code as below:
1 using System;
2 using System.Diagnostics;
3 using System.Windows.Forms;
4 using System.IO;
5 using System.Threading;
6 namespace RunCommand
7 {
8 public partial class FrmMain : Form
9 {
10 Thread newThread = null;
11 Process proc = null;
12
13 public FrmMain()
14 {
15 InitializeComponent();
16 }
17
18 private void btnRun_Click(object sender, EventArgs e)
19 {
20 string cmd = Path.Combine(Application.StartupPath, "myfile.exe");
21 string args = "notepad";
22
23 CommandObject co = new CommandObject();
24 co.FileName = cmd;
25 co.Arguments = args;
26 newThread = new Thread(new ParameterizedThreadStart(RunCommand));
27 newThread.Start(co);
28 }
29
30 private void RunCommand(object commandObject)
31 {
32 CommandObject co = commandObject as CommandObject;
33 string ret = String.Empty;
34 if (co != null)
35 {
36 proc = new Process();
37
38 ProcessStartInfo startInfo = new ProcessStartInfo(co.FileName);
39 startInfo.Arguments = co.Arguments;
40 startInfo.RedirectStandardOutput = true;
41 startInfo.RedirectStandardInput = false;
42 startInfo.RedirectStandardError = true;
43 startInfo.UseShellExecute = false;
44 startInfo.CreateNoWindow = false;
45
46 proc.StartInfo = startInfo;
47 proc.Start();
48 proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
49 proc.BeginOutputReadLine();
50 proc.WaitForExit();
51 }
52 }
53
54 private void proc_OutputDataReceived(object Sender, DataReceivedEventArgs e)
55 {
56 if (e.Data != null)
57 {
58 AddRedirectOutput(e.Data);
59 }
60 }
61
62 delegate void RefreshForm(string s1);
63 //the method that changes UI out of main thread
64 private void AddRedirectOutput(string output)
65 {
66 if (txtOutput.InvokeRequired)
67 {
68 Invoke(new RefreshForm(AddRedirectOutput), output);
69 }
70 else
71 {
72 txtOutput.AppendText(string.Format("{0}{1}",output,Environment.NewLine));
73 }
74 }
75
76
77 private class CommandObject
78 {
79 public string FileName
80 {
81 get;
82 set;
83 }
84
85 public string Arguments
86 {
87 get;
88 set;
89 }
90 }
91
92 private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
93 {
94 if (proc != null && !proc.HasExited)
95 {
96 proc.Kill();
97 }
98 if (newThread != null)
99 {
100 newThread.Abort();
101 }
102 }
103 }
104 }
1. Application.StartupPath -> Gets the path for the executable file that started the application, not including the executable name.
2. Path.Combine -> Combines two strings into a path.
3. ParameterizedThreadStart -> Represents the method that executes on a Thread. obj contains data for the thread procedure.
[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart (Object obj)
4. Thread -> Creates and controls a thread, sets its priority, and gets its status.
5. Process.OutputDataReceived -> Occurs when an application writes to its redirected StandardOutput stream.
The event is enabled during asynchronous read operations on StandardOutput. To start asynchronous read operations, you must redirect the StandardOutput stream of a Process, add your event handler to the OutputDataReceived event, and call BeginOutputReadLine. Thereafter, the OutputDataReceived event signals each time the process writes a line to the redirected StandardOutput stream, until the process exits or calls CancelOutputRead.
public delegate void DataReceivedEventHandler(Object sender, DataReceivedEventArgs e)
6. Process.BeginOutputReadLine -> Begins asynchronous read operations on the redirected StandardOutput stream of the application.
7. Process.WaitForExit -> Instructs the Process component to wait indefinitely for the associated process to exit.
8. DataReceivedEventArgs.Data -> Gets the line of characters that was written to a redirected Process output stream.
9. Control.InvokeRequired -> Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on.
10. Control.Invoke -> Executes the specified delegate on the thread that owns the control's underlying window handle.