none
How to check if a Pipe is listening or not? RRS feed

  • Question

  • Hello...

    I have developed a Windows service application which communicates with a Windows Forms application by mean of named pipes.The Windows Forms application start listening waiting for connections asynchronously from the service. All works perfectly, however, when the Windows Form application is not running (which will be the most common situation), the service waits and then sends a timeout error. I have set currently the timeout to 2000 ms. Lower value causes the system not to work.

    How can the service test if the pipe is listening before sending the message to it?

    This is the function I am using to send the message from the service to the windows form application:

            public void Send(string sendStr, string pipeName, int timeOut = 2000)
            {
                try
                {
                    _pipeName = pipeName;
    
                    NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous);
    
                    // The connect function will indefinitely wait for the pipe to become available
                    // If that is not acceptable specify a maximum waiting time (in ms)
                    pipeStream.Connect(timeOut);
                    //Debug.WriteLine("[Client] Pipe connection established");
    
                    byte[] _buffer = Encoding.UTF8.GetBytes(sendStr);
                    pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
    
                    //Debug.WriteLine("[Client] " + SendStr);
                }
                catch (Exception oEX)
                {
                    Debug.WriteLine(oEX.Message);
                    OnError(oEX, pipeName);
                }
            }
    

    Any advice will be greatly appreciated

    Thanks

    Jaime


    Powered by C++

    Thursday, January 9, 2014 2:00 PM

Answers

  • The you have to check if the process is running.  It is similar to looking at task manager to see if the process is running.  You use GetProcessById and search by process name.  if it is on a remote machine then you need access rights to the remove machine and perform the equivalent over the network.

    jdweng

    Thursday, January 9, 2014 2:21 PM

All replies

  • You could have the application send back a message.  Usually you would have some sort of command language or have the application echo back the send message.  if you don't get any response then it is closed.

    jdweng

    Thursday, January 9, 2014 2:10 PM
  • thanks, but if the application is not running, we are getting the same problem. The service must be aware whether it can send the message or not.

    Regards,

    Jaime


    Powered by C++

    Thursday, January 9, 2014 2:14 PM
  • The you have to check if the process is running.  It is similar to looking at task manager to see if the process is running.  You use GetProcessById and search by process name.  if it is on a remote machine then you need access rights to the remove machine and perform the equivalent over the network.

    jdweng

    Thursday, January 9, 2014 2:21 PM
  • Hello,

    I have created this function in the service:

            private bool IsMonitorRunning()
            {
                try
                {
                    Process[] monitorView = Process.GetProcessesByName("MonitorView");
                    if (monitorView.Length == 0)
                        return false;
    
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    

    I will call that function everywhere before sending the message to the Windows Form application, which is called "MonitorView".

    Thanks

    Jaime


    Powered by C++

    • Proposed as answer by dns jinung Friday, January 17, 2014 10:16 AM
    Thursday, January 9, 2014 3:51 PM
  • so you have solved your problem?
    Friday, January 17, 2014 10:17 AM