Use Visual C # To implement the Window pipeline technology

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.