Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:09:48+00:00 2026-05-29T22:09:48+00:00

I’m trying to put together a class to handle Ipc between processes using anonymous

  • 0

I’m trying to put together a class to handle Ipc between processes using anonymous pipes provided by System.Io.Pipes.

The problem I’m having is that when I test the class using a single process the pipes set up correctly and I can send data between client and server without a problem. However, when I split the client and server into separate processes ( on the same machine ), the client is unable to connect to the end of the server pipe.

The error System.Io.Exception Invalid pipe handle is raised when call

_outboundPipeServerStream = new AnonymousPipeClientStream(PipeDirection.Out, serverHandle);

The full code of the class is pasted below.

Essentially its work like this;

  1. Server Process. Create anonymous pipe set for inbound data – call this Pipe A
  2. Server Process. Starts Client process and passes PipeHandle via command argument
  3. Client Process. Connects to end of Pipe A
  4. Client Process. Create anonymous pipe set for inbound data (Pipe B)
    5 Client process. Passes pipe handle back to Server using Pipe A
  5. Server Process. Connects to end of Pipe B

So now we have two anonymous pipes, pointing in opposite directions between Server and Client.

Here is the full code of my IPC class

    public class MessageReceivedEventArgs : EventArgs
{
    public string Message { get; set; }
}

public class IpcChannel : IDisposable
{
    private AnonymousPipeServerStream _inboundPipeServerStream;
    private StreamReader _inboundMessageReader;
    private string _inboundPipeHandle;

    private AnonymousPipeClientStream _outboundPipeServerStream;
    private StreamWriter _outboundMessageWriter;

    public delegate void MessageReceivedHandler(object sender, MessageReceivedEventArgs e);
    public event MessageReceivedHandler MessageReceived;

    private Thread _clientListenerThread;
    private bool _disposing = false;

    public IpcChannel()
    {
        SetupServerChannel();
    }

    public IpcChannel(string serverHandle)
    {
        SetupServerChannel();
        // this is the client end of the connection

        // create an outbound connection to the server
        System.Diagnostics.Trace.TraceInformation("Connecting client stream to server : {0}", serverHandle);
        SetupClientChannel(serverHandle);

        IntroduceToServer();
    }

    private void SetupClientChannel(string serverHandle)
    {
        _outboundPipeServerStream = new AnonymousPipeClientStream(PipeDirection.Out, serverHandle);
        _outboundMessageWriter = new StreamWriter(_outboundPipeServerStream)
        {
            AutoFlush = true
        };
    }

    private void SetupServerChannel()
    {
        _inboundPipeServerStream = new AnonymousPipeServerStream(PipeDirection.In);
        _inboundMessageReader = new StreamReader(_inboundPipeServerStream);
        _inboundPipeHandle = _inboundPipeServerStream.GetClientHandleAsString();
        _inboundPipeServerStream.DisposeLocalCopyOfClientHandle();

        System.Diagnostics.Trace.TraceInformation("Created server stream " + _inboundPipeServerStream.GetClientHandleAsString());

        _clientListenerThread = new Thread(ClientListener)
        {
            IsBackground = true
        };

        _clientListenerThread.Start();

    }

    public void SendMessage(string message)
    {
        System.Diagnostics.Trace.TraceInformation("Sending message {0} chars", message.Length);

        _outboundMessageWriter.WriteLine("M" + message);
    }

    private void IntroduceToServer()
    {
        System.Diagnostics.Trace.TraceInformation("Telling server callback channel is : " + _inboundPipeServerStream.GetClientHandleAsString());

        _outboundMessageWriter.WriteLine("CI" + _inboundPipeServerStream.GetClientHandleAsString());
    }

    public string ServerHandle
    {
        get
        {
            return _inboundPipeHandle;
        }
    }

    private void ProcessControlMessage(string message)
    {
        if (message.StartsWith("CI"))
        {
            ConnectResponseChannel(message.Substring(2));
        }
    }

    private void ConnectResponseChannel(string channelHandle)
    {
        System.Diagnostics.Trace.TraceInformation("Connecting response (OUT) channel to : {0}", channelHandle);

        _outboundPipeServerStream = new AnonymousPipeClientStream(PipeDirection.Out, channelHandle);
        _outboundMessageWriter = new StreamWriter(_outboundPipeServerStream);
        _outboundMessageWriter.AutoFlush = true;
    }

    private void ClientListener()
    {
        System.Diagnostics.Trace.TraceInformation("ClientListener started on thread {0}", Thread.CurrentThread.ManagedThreadId);

        try
        {
            while (!_disposing)
            {
                var message = _inboundMessageReader.ReadLine();
                if (message != null)
                {
                    if (message.StartsWith("C"))
                    {
                        ProcessControlMessage(message);
                    }
                    else if (MessageReceived != null)
                        MessageReceived(this, new MessageReceivedEventArgs()
                        {
                            Message = message.Substring(1)
                        });
                }
            }
        }
        catch (ThreadAbortException)
        {
        }
        finally
        {

        }
    }

    public void Dispose()
    {
        _disposing = true;

        _clientListenerThread.Abort();

        _outboundMessageWriter.Flush();
        _outboundMessageWriter.Close();
        _outboundPipeServerStream.Close();
        _outboundPipeServerStream.Dispose();

        _inboundMessageReader.Close();
        _inboundMessageReader.Dispose();

        _inboundPipeServerStream.DisposeLocalCopyOfClientHandle();
        _inboundPipeServerStream.Close();
        _inboundPipeServerStream.Dispose();
    }
}

In a single process, it can be used like this;

class Program
{
    private static IpcChannel _server;
    private static IpcChannel _client;

    static void Main(string[] args)
    {
        _server = new IpcChannel();
        _server.MessageReceived += (s, e) => Console.WriteLine("Server Received : " + e.Message);

        _client = new IpcChannel(_server.ServerHandle);
        _client.MessageReceived += (s, e) => Console.WriteLine("Client Received : " + e.Message);


        Console.ReadLine();

        _server.SendMessage("This is the server sending to the client");

        Console.ReadLine();

        _client.SendMessage("This is the client sending to the server");

        Console.ReadLine();

        _client.Dispose();
        _server.Dispose();
    }

Thanks in advance for any suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:09:50+00:00Added an answer on May 29, 2026 at 10:09 pm

    You didn’t post the server code, but anyway. In the server:

    • You need to specify that the client’s pipe handle is inheritable when you create it.
    • When you launch the client you need to specify that inheritable handles will be inherited.

    If you miss either of these steps then the pipe handle will be invalid in the client process.

    Also, your step 4 won’t work. If you create a pipe handle in the client it won’t mean anything to the server when you pass it back. You can make this work using the DuplicateHandle function, but it’s much easier to create all the handles in the server and inherit them in the client.

    The key point is that handles are per-process, not system-wide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.