[Original] C # Implement Window Pipeline Technology

xiaoxiao2021-03-06  18

Previously, I used the Window API to implement the articles of pipe technology. Later, I later changed the same effect. I found that the C # itself has a convenient process thread mechanism makes the work simple to extreme, and record it.

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 AutoreteTevent (false); AutoreteTevent [] Events = New AutoRetevent [1]; Events [0] = m_Eventoutput;

M_threadOutput = New Thread (new threadstart (new threadstart); m_threadoutput.start (); waithandle.waitall (events);

The thread function is 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 "/ R / N "); m_txtoutput.selectionStart = m_txtoutput.text.length; m_txtoutput.scrolltocaret ();} thread.sleep (100);} m_eventoutput.set ();

It should be noted here that using the following statement to make TextBox display always updated, and appendtext instead of = because = will cause the entire TextBox to make the entire display area flashes throughout the display area

M_txtOutput.AppendText (Strline "/ R / N"); 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 another thread.

转载请注明原文地址:https://www.9cbs.com/read-44417.html

New Post(0)