⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 管道.txt

📁 该压缩包里主要写了几个关于如何将文本数据加密解密序列化的实现
💻 TXT
字号:
操作系统中负责线程间通讯的东西叫管道。 

管道(pipe)是进程用来通讯的共享内存区域。一个进程往管道中写入信息,而其它的进程可以从管道中读出信息。如其名,管道是进程间数据交流的通道。 

管道技术一般采用Window API来实现,但C#本身方便的进程线程机制使工作变得简单. 
首先,我们可以通过设置Process类,获取输出接口,代码如下: 

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(); 

然后设置线程连续读取输出的字符串: 
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 ); 

线程函数如下: 
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(); 
} 


m_txtOutput.AppendText( strLine + "\r\n" ); 
m_txtOutput.SelectionStart = m_txtOutput.Text.Length; 
m_txtOutput.ScrollToCaret(); 
为了不阻塞主线程,可以将整个过程放到一个另一个线程里就可以了




首先,我们可以通过设置Process类,获取输出接口,代码如下:
 

1  Process proc = new Process(); 
2  proc .StartInfo.FileName = strScript; 
3  proc .StartInfo.WorkingDirectory = strDirectory; 
4  proc .StartInfo.CreateNoWindow = true; 
5  proc .StartInfo.UseShellExecute = false; 
6  proc .StartInfo.RedirectStandardOutput = true; 
7  proc .Start();



然后设置线程连续读取输出的字符串:
 

1  eventOutput = new AutoResetEvent(false); 
2  AutoResetEvent[] events = new AutoResetEvent[1]; 
3  events[0] = m_eventOutput; 
4
5  m_threadOutput = new Thread( new ThreadStart( DisplayOutput ) ); 
6  m_threadOutput.Start(); 
7  WaitHandle.WaitAll( events );



 线程函数如下:
 

 1  private void DisplayOutput() 
 2  { 
 3   while ( m_procScript != null && !m_procScript.HasExited ) 
 4   { 
 5   string strLine = null; 
 6   while ( ( strLine = m_procScript.StandardOutput.ReadLine() ) != null) 
 7   { 
 8    m_txtOutput.AppendText( strLine + "\r\n" ); 
 9    m_txtOutput.SelectionStart = m_txtOutput.Text.Length; 
10    m_txtOutput.ScrollToCaret(); 
11   } 
12   Thread.Sleep( 100 ); 
13   } 
14   m_eventOutput.Set(); 
15  }



 这里要注意的是,使用以下语句使TextBox显示的总是最新添加的,而AppendText而不使用+=,是因为+=会造成整个TextBox的回显使得整个显示区域闪烁 
 

1  m_txtOutput.AppendText( strLine + "\r\n" ); 
2  m_txtOutput.SelectionStart = m_txtOutput.Text.Length; 
3  m_txtOutput.ScrollToCaret();
4

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -