Pipeline Technology is generally implemented using the Window API. Recently I tried to use C # To implement the Windows pipeline technology. I found that the convenient process thread MECHANISM OF C # made the work extremely simple and easily recorded, we recommend it to you.
First, we can get the output interface by setting the Process class. The Code is as follows:
Process proc = new Process (); Proc. StartInfo. FileName = strScript; Proc. StartInfo. WorkingDirectory = strDirectory; Proc. StartInfo. CreateNoWindow = true; Proc. StartInfo. UseShellExecute = false; Proc. StartInfo. RedirectStandardOutput = true; Proc. Start (); |
Then set the thread to continuously read the output string:
EventOutput = new AutoResetEvent (false ); AutoResetEvent [] events = new AutoResetEvent [1]; Events [0] = m_eventOutput;
M_threadOutput = new Thread (new ThreadStart (DisplayOutput )); M_threadOutput.Start (); WaitHandle. WaitAll (events ); |
The thread functions are as follows:
Private void DisplayOutput () { While (m_procScript! = Null &&! M_procScript.HasExited) { String strLine = null; While (strLine = m_procScript.StandardOutput.ReadLine ())! = Null) { M_txtOutput.AppendText (strLine + ""); M_txtOutput.SelectionStart = m_txtOutput.Text.Length; M_txtOutput.ScrollToCaret (); } Thread. Sleep (100 ); } M_eventOutput.Set (); } |
Note that the following statements are used to make TextBox display always the latest, while AppendText does not use + =, it is because + = causes the display of the entire TextBox to flash the entire display area.
M_txtOutput.AppendText (strLine + ""); M_txtOutput.SelectionStart = m_txtOutput.Text.Length; M_txtOutput.ScrollToCaret (); |
In order not to block the main thread, you can put the whole process in one thread or another.