zoukankan      html  css  js  c++  java
  • AnonymousPipeStream的使用案例

    AnonymousPipeStream的使用具体案例如下:

    服务端:

    using System;
    using System.Data;
    using System.Data.SQLite;
    using System.IO;
    using System.IO.Pipes;
    using System.Net;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace SupremeConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                #region 测试  NamedPipeStream
                 new Thread(new ThreadStart(AnonymousPipeServer)).Start();
                #endregion
             } 
    
            public static void AnonymousPipeServer()
            {
                string clientExe = @"F:PersonLongtengLongtengSlnConsoleAppTestAnonymousPipeinDebugConsoleAppTestAnonymousPipe.exe";
                HandleInheritability inherit = HandleInheritability.Inheritable;
                using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit))
                using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit))
                {
                    txID = tx.GetClientHandleAsString();
                    rxID = rx.GetClientHandleAsString();
                    var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID);
                    startInfo.UseShellExecute = false; // Required for child process
                    Process p = Process.Start(startInfo);
                    tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged
                    rx.DisposeLocalCopyOfClientHandle(); // handle resources.
                    //tx.WriteByte(100);
                    //Console.WriteLine("Server received: " + rx.ReadByte());
                    //p.WaitForExit();    
                    while (true)
                    {
                        tx.WriteByte(100);
                        Console.WriteLine("Server received: " + rx.ReadByte());
                    }
                               
                }
            }
         }
    }

    客户端(新建一个控制台程序):

    using System;
    using System.IO.Pipes;
    using System.Threading;
    
    namespace ConsoleAppTestAnonymousPipe
    {
        class Program
        {
            static void Main(string[] args)
            {
                string rxID = args[0]; // Note we're reversing the
                string txID = args[1]; // receive and transmit roles.
                using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID))
                using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID))
                {
                    //Console.WriteLine("Client received: " + rx.ReadByte());
                    //tx.WriteByte(200);
                    while (true)
                    {
                        Console.WriteLine("Client received: " + rx.ReadByte());
                        tx.WriteByte(200);
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }
            }
        }
    }

    运行结果:

  • 相关阅读:
    Find all Windows Phone Application Bar Icon
    DELL PowerConnect 5548[初步]配置
    删除SD的分区
    Import/Export a database into Oracle
    索爱U100i排线更换笔记
    forrtl: severe (174): SIGSEGV, segmentation fault occurred
    下载功能和上传功能的实现
    自我小结:手动给GridView添加数据源
    删除指定目录内的文件
    GridView数据源中没有数据的时候显示表头
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/11911299.html
Copyright © 2011-2022 走看看