Example #1
0
        static void ActivateAnotherInstance(string npname)
        {
            using (var nps = new System.IO.Pipes.NamedPipeClientStream(".", npname
                                                                       , System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.Asynchronous | System.IO.Pipes.PipeOptions.WriteThrough
                                                                       , System.Security.Principal.TokenImpersonationLevel.Anonymous))
            {
                MemoryStream ms = new MemoryStream();
                try
                {
                    nps.Connect(2000);

                    byte[] buffer = new byte[1024];
                    while (nps.IsConnected)
                    {
                        int rc;
                        try
                        {
                            rc = nps.Read(buffer, 0, buffer.Length);
                        }
                        catch (IOException)
                        {
                            break;
                        }
                        if (rc == 0)
                        {
                            Thread.Sleep(10);
                            continue;
                        }
                        WriteDebugLine("read data size " + rc);
                        if (rc != 0)
                        {
                            ms.Write(buffer, 0, rc);
                        }
                    }

                    nps.Close();
                }
                catch (Exception x)
                {
                    WriteDebugLine(x);
                }
                string msg = System.Text.Encoding.ASCII.GetString(ms.ToArray());
                WriteDebugLine("ActivateAnotherInstance " + msg);
            }
        }