NamedPipeClientStream client;
StreamWriter writer;
StreamReader reader;
string name;
public ClientNamedPipe(string name)
{
this.name = name;
Task.Factory.StartNew(() =>
{
while(true)
{
MessageBox.Show($"Создаём клиента {name}");
StartClient();
string a = reader.ReadLine();
Dispose();
MessageBox.Show($"Клиент сломался.");
}
}, TaskCreationOptions.LongRunning);
}
private void StartClient()
{
client = new NamedPipeClientStream(name);
client.Connect();
writer = new StreamWriter(client) { AutoFlush = true };
reader = new StreamReader(client);
}
public void Send(string message)
{
try
{
writer?.WriteLine(message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Dispose()
{
try
{
MessageBox.Show("чистим");
reader?.Dispose();
writer?.Dispose();
client?.Dispose();
reader = null;
writer = null;
client = null;
MessageBox.Show("почистили");
}
catch (Exception ex)
{
MessageBox.Show("StopClient " + ex.Message);
}
}
}
Мес.бокс из диспосабле убери
Обсуждают сегодня