Иногда, на некоторых машинах часто и на некоторых, клиенты, которые используют мою программу, получают исключение "канал закрывается". Это происходит на NamedPipeServerStream, который находится на .WaitForConnection (). После этого приложение полностью падает и выдает исключение Windows. Это происходит, когда NamedPipeClientStream передает информацию в автономное приложение.
Основные функции:
Я написал несколько Инструментов (Панели инструментов Office, Сервис, отдельное приложение .net и небольшой стартовый exe), которые взаимодействуют вместе с NamedPipes.
Служба запускает NamedPipeServerStream, который всегда открыт (в состоянии .WaitForConnection ();), а у Автономного приложения также есть NamedPipeServerStream.
Панели инструментов и стартер .exe взаимодействуют с сервисом. Служба с автономным приложением.
какие проблемы могут высвободить трубу закрывается за исключением?
Возможно ли, что сервер отправляет информацию Автономному приложению, но закрывает поток рано, потому что Автономное приложение не готово или что-то еще? на каждом NamedPipeClientStream я делаю waitforpipedrain, если pipeClient.IsConnected, прежде чем я закрою pipeclient ..
спасибо за помощь
edit: вот пример клиентского потока
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();
}
Пример pipeserver (который работает в отдельном потоке)
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();