Implementing Windows Piping Technology with Visual C #

Source: Internet
Author: User
Visual|window

Pipeline technology is generally implemented using the window API, I recently tried to use C # to implement the Windows pipeline technology, found that C # itself convenient process threading mechanism to make the work is very simple, and record, recommended to everyone.

First, we can get the output interface by setting the process class, and 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 string that the thread reads output sequentially:

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 + "\r\n" );
    m_txtOutput.SelectionStart = m_txtOutput.Text.Length;
    m_txtOutput.ScrollToCaret();
   }
   Thread.Sleep( 100 );
  }
  m_eventOutput.Set();
}

Note here that the following statement is used to make the textbox display always the most recently added, and appendtext instead of + = because = = causes the entire textbox to be echoing so that the entire display area flashes

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 on a different line Chengri.



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.