原创  c#进程间的通信 收藏

 ProcessTestWrite.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace ConsoleTest
{
    
class ProcessTestWrite
    {
        Process p 
= null;
        
public ProcessTestWrite()
        {
            p 
= new Process();
            p.StartInfo.CreateNoWindow 
= true;
            p.StartInfo.UseShellExecute 
= false;
            p.StartInfo.RedirectStandardInput 
= true;
            p.StartInfo.RedirectStandardOutput 
= true;
            p.StartInfo.RedirectStandardError 
= true;
            p.StartInfo.FileName 
= "ProcessTestRead.exe";
            p.Start();
            
new Thread(this.run).Start();
        }

        
public static void Main(String[] args)
        {
            ProcessTestWrite rtw 
= new ProcessTestWrite();
            rtw.Send();
        }

        
public void Send()
        {
            StreamWriter sw 
= p.StandardInput;
            
while (true)
            {
                sw.WriteLine(
"jxncwzb");
            }
        }

        
public void run()
        {
            StreamReader sr 
= p.StandardOutput;
            
while (true)
            {
                String strLine 
= sr.ReadLine();
                
if (strLine != null)
                    Console.WriteLine(strLine);
                
else
                    
return;
            }
        }
    }
}

ProcessTestRead.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleTest
{
    
class ProcessTestRead
    {
        
static void Main(string[] args)
        {
            TextReader tr 
= Console.In;
            
while (true)
            {
                
string str = tr.ReadLine();
                
if (str!=null)
                    Console.WriteLine(str
+":你好");
                
else
                    
return;
            }
        }
    }
}

用ProcessTestWrite.exe写入信息,用ProcessTestRead.exe读取信息
运行ProcessTestWrite.exe看看

发表于 @ 2007年05月09日 13:40:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:c#线程间的通信 | 新一篇:用XmlTextWriter追加记录

  • 发表评论
  • 评论内容:
  •  
Copyright © jxncwzb
Powered by CSDN Blog