2012-02-07 204 views
0

時には、一部のマシンで、まれにしかプログラムを使用していないクライアントが「パイプが閉じられています」例外が発生することがあります。これは、.WaitForConnection()にあるNamedPipeServerStreamで発生します。その後、アプリケーションは完全にクラッシュし、Windows例外を解放します。これは、NamedPipeClientStreamがスタンドアロンアプリケーションに情報を転送するときに発生します。パイプが閉じられている例外例外

主な機能: NamedPipeと一緒に通信するいくつかのツール(Officeツールバー、サービス、スタンドアロンの.netアプリケーション、およびlitleスターターexe)を書きました。 サービスは、常に開いている状態(.WaitForConnection();)のNamedPipeServerStreamを実行し、スタンドアロンアプリケーションもNamedPipeServerStreamを持ちます。 ツールバーとスターター.exeがサービスと通信します。スタンドアロンアプリケーションでのサービス

どのような種類の問題をパイプを解放できるかどうかは例外ですか? スタンドアロンアプリケーションに情報を送信するが、スタンドアロンアプリケーションが準備されていないため、ストリームを早期に閉じることは可能ですか?ここpipeserverのclientstream

using (NamedPipeClientStream pipeClient = 
     new NamedPipeClientStream(".", pipename, PipeDirection.Out)) 
{ 
    // Wait for a client to connect 
    try 
    {        
     pipeClient.Connect(3000); 

     // send params to the form 
     using (StreamWriter sw = new StreamWriter(pipeClient)) 
     { 
      sw.AutoFlush = true; 
      sw.WriteLine(sendtext); 
     } 
    } 
    // Catch the IOException that is raised if the pipe is 
    // broken or disconnected. 
    catch (Exception e) 
    { 
     if (sid != "") 
     { 
      connections.Remove(conName); 
     } 
     eventLog1.WriteEntry("SendText Fehler 1 " + e.Message); 
    } 
    finally 
    { 
     if (pipeClient.IsConnected) 
     { 
      pipeClient.WaitForPipeDrain(); 
     } 
     pipeClient.Close(); 
     pipeClient.Dispose(); 
    } 

例の例(その:各NamedPipeClientStreamに私は

編集私はpipeclientを閉じるbefor pipeClient.IsConnected場合waitforpipedrain ..助けを

感謝を行いますseperadスレッドで実行)

NamedPipeServerStream pipeServer; 
PipeSecurity pipe_security = CreateSystemIoPipeSecurity(); 
do 
    string pipename = global::TOfficeCenter.Properties.Settings.Default.pipename; 
    string aText = ""; 
    pipeServer = new NamedPipeServerStream(pipename, PipeDirection.In, ONE_INSTANCE, PipeTransmissionMode.Byte, 
            PipeOptions.None, IN_BUF_SIZE, OUT_BUF_SIZE, pipe_security); 
    try 
    { 
     // Verbindung zu TOfficeCenter.exe aufbauen 
     try 
     { 
      IsWaiting = true; 
      pipeServer.WaitForConnection(); 
      IsWaiting = false; 

      using (StreamReader sr = new StreamReader(pipeServer)) 
      { 
       string temp; 
       while ((temp = sr.ReadLine()) != null) 
       { 
        aText = aText + temp; 
       } 
      } 

      try 
      { 
       if (aText == "") 
       { 
        empfang(null); 
       } 
       else 
       { 
        if (aText != "KillPipe") 
        { // XML empfangen 
         XmlDocumentTC xml = new XmlDocumentTC(); 
         xml.LoadXml(aText); 
         empfang(xml); 
        } 
       } 
      } 
      catch (Exception) 
      { 
       empfang(null); 
      } 
     } 
     catch 
{........... 
}  
    } 

    catch (Exception e) 
    {........... 
    } 

} while (running); 

pipeServer.Close(); 
+0

いずれかのアプリケーションでパイプが偶然閉じられていませんか? – svick

+0

私は、クライアントデータがサーバ上で例外を起こしていると思います。これは未処理のため、サーバーがシャットダウンし、その例外がクライアントで生成されます。サーバーの例外ログを改善してこれを診断し、AppDomain.UnhandledExceptionイベントを実装します。 –

+0

あなたの答えをありがとう。 – user1194383

答えて

0

それは私が最終的に問題を発見している可能性があります。..

私はこのコードの後ことを発見:

    using (StreamWriter sw = new StreamWriter(pipeClient)) 
        { 
         sw.AutoFlush = true; 
         sw.WriteLine(sendtext); 
        } 

pipeClient.IsConnected()。 WaitForPipeDrainに決して来ないように、直接falseを返します。私は今

    using (StreamWriter sw = new StreamWriter(pipeClient)) 
        { 
         sw.AutoFlush = true; 
         sw.WriteLine(sendtext); 
         pipeClient.WaitForPipeDrain(); 
        } 

は、あなたはそれが問題を解決している可能性が考えてください。..そのようにそれをやったし、サーバーが読み取りを終了する前に、クライアントが接続を閉じていないことを願っていますか?私はそれをして以来、私は2つのテストマシンでエラーを得たことはありません。しかし、エラーがとにかくめったに起こらなかった。..

0

私の使用は少し異なっているが、それは主に現在のMSDNのページからハッキングされていますように私は合計でサーバーのスレッドが含まれます:

MSDN: How to Use Named Pipes

私には "WaitForPipeToDrain()"が必要かどうかは分かりませんが、コードから取りました:)

私はpipeServerをリセットするたびにIOExceptionを解決したと思います。

int threadId = Thread.CurrentThread.ManagedThreadId; 
    bool sentinel = true; 

    while (sentinel) 
    { 
     NamedPipeServerStream pipeServer = 
      new NamedPipeServerStream("shapspipe", PipeDirection.InOut, 1); 
     // Wait for a client to connect 
     pipeServer.WaitForConnection(); 

     Console.WriteLine("Client connected on thread[{0}].", threadId); 
     try 
     { 
      // Read the request from the client. Once the client has 
      // written to the pipe its security token will be available. 

      StreamString ss = new StreamString(pipeServer); 

      // Verify our identity to the connected client using a 
      // string that the client anticipates. 

      ss.WriteString("I am the one true server!"); 
      string message = ss.ReadString(); 

      Console.WriteLine("received from client: " + message); 

      ss.WriteString("echo from server: " + message); 

      Console.WriteLine("Received from client: {0} on thread[{1}] as user: {2}.", 
       message, threadId, pipeServer.GetImpersonationUserName()); 
     } 
     // Catch the IOException that is raised if the pipe is broken 
     // or disconnected. 
     catch (IOException e) 
     { 
      Console.WriteLine("ERROR: {0}", e.Message); 
     } 
     pipeServer.WaitForPipeDrain(); 
     pipeServer.Close(); 
    } 
関連する問題