NamedPipeServerStream 读取器/写入器线程 C#

发布于 2024-12-11 16:12:38 字数 1543 浏览 0 评论 0原文

我试图在两个线程(读取器和写入器)之间共享 NamedPipeServerStream 实例。该程序可以工作,但是当我尝试创建 ServerProcess 对象的两个实例时,编写器线程开始挂起。我哪里错了?

任何建议将不胜感激。

生成方法如下:

 public class ServerPrcoess
 {
    private static NamedPipeServerStream m_NamePipeHandle;
    public  static void StartPipe()
    {
        int i = 0;

        Thread[] OPCServerReader = new Thread();
        Thread[] OPCServerWriter = new Thread();

        m_NamePipeHandle = new NamedPipeServerStream(PIPENAME, PipeDirection.InOut, 254, PipeTransmissionMode.Message, PipeOptions.Asynchronous);

        m_NamePipeHandle.WaitForConnection();

        OPCServerReader = new Thread(PipeReaderThread);
        OPCServerReader.IsBackground = true;
        OPCServerReader.Start(i);

        Thread.Sleep(1000);

        OPCServerWriter = new Thread(PipeWriterThread);
        OPCServerWriter.IsBackground = true;
        OPCServerWriter.Start();
      }
 }

读取器线程与下面类似,并使用管道句柄创建二进制读取器对象:-

private  static void PipeReaderThread(object ThreadNo)
{
     Console.WriteLine("Reader Thread Number: {0}", (int)ThreadNo);
     try
     {

         StreamOPCMessageReader oMsgReader = new StreamOPCMessageReader(m_NamePipeHandle);

            ...

写入器线程使用相同的管道句柄执行等效操作。

private static void PipeWriterThread(object _ThreadNo)
{         
     int ThreadNo  = Thread.CurrentThread.ManagedThreadId;
     try
     {               
        StreamOPCMessageWriter oMsgWriter = new StreamOPCMessageWriter(m_NamePipeHandle);

I’m attempting to share a NamedPipeServerStream instance between two threads, a reader and a writer. The program works but when I attempt to create two instances of the ServerProcess object the writer thread starts hanging. Where am I going wrong?

Any suggestions will be greatly appreciated.

The spawning method is as follows:

 public class ServerPrcoess
 {
    private static NamedPipeServerStream m_NamePipeHandle;
    public  static void StartPipe()
    {
        int i = 0;

        Thread[] OPCServerReader = new Thread();
        Thread[] OPCServerWriter = new Thread();

        m_NamePipeHandle = new NamedPipeServerStream(PIPENAME, PipeDirection.InOut, 254, PipeTransmissionMode.Message, PipeOptions.Asynchronous);

        m_NamePipeHandle.WaitForConnection();

        OPCServerReader = new Thread(PipeReaderThread);
        OPCServerReader.IsBackground = true;
        OPCServerReader.Start(i);

        Thread.Sleep(1000);

        OPCServerWriter = new Thread(PipeWriterThread);
        OPCServerWriter.IsBackground = true;
        OPCServerWriter.Start();
      }
 }

The reader thread is similar to below and creates a binary reader object using the pipe handle :-

private  static void PipeReaderThread(object ThreadNo)
{
     Console.WriteLine("Reader Thread Number: {0}", (int)ThreadNo);
     try
     {

         StreamOPCMessageReader oMsgReader = new StreamOPCMessageReader(m_NamePipeHandle);

            ...

And the writer thread does the equivalent using the same pipe handle.

private static void PipeWriterThread(object _ThreadNo)
{         
     int ThreadNo  = Thread.CurrentThread.ManagedThreadId;
     try
     {               
        StreamOPCMessageWriter oMsgWriter = new StreamOPCMessageWriter(m_NamePipeHandle);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-12-18 16:12:38

您无法创建多个具有相同名称 (PIPENAME) 的 NamedPipeServerStream 实例。

You can not create multiple instances of NamedPipeServerStream with the same name(PIPENAME).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文