如何关闭名为pipeclementeam?
我无法关闭WPF应用程序,因为名为PipeCleentStream无限运行。我尝试使用connectAsync
,我都尝试将其处置,但是无论它是什么阻止了该过程结束。
我使用管道收听消息,因此需要运行直到应用程序退出。
这是我目前的尝试:
public MainWindow()
{
namedPipeClient = new(System.Configuration.ConfigurationManager.AppSettings["AppGUID"]!);
Thread PipeThread = new(() =>
{
while (true)
{
try
{
PipeReceive();
}
catch (TimeoutException) { }
catch (ObjectDisposedException) { break; }
}
});
PipeThread.Start();
}
// Called when window is closed
protected override void OnClosed(EventArgs e)
{
namedPipeClient.Close();
namedPipeClient.Dispose();
base.OnClosed(e);
}
private void PipeReceive()
{
namedPipeClient.Connect(1000);
if (namedPipeClient.IsConnected)
{
// Retrieve message
}
}
I can't close my WPF application because the NamedPipeClientStream endlessly runs. I tried using ConnectAsync
, I tried disposing it, but no matter what it prevents the process from ending.
I use the pipe to listen for messages so it needs to run until the application quits.
This is my current attempt:
public MainWindow()
{
namedPipeClient = new(System.Configuration.ConfigurationManager.AppSettings["AppGUID"]!);
Thread PipeThread = new(() =>
{
while (true)
{
try
{
PipeReceive();
}
catch (TimeoutException) { }
catch (ObjectDisposedException) { break; }
}
});
PipeThread.Start();
}
// Called when window is closed
protected override void OnClosed(EventArgs e)
{
namedPipeClient.Close();
namedPipeClient.Dispose();
base.OnClosed(e);
}
private void PipeReceive()
{
namedPipeClient.Connect(1000);
if (namedPipeClient.IsConnected)
{
// Retrieve message
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题与命名管道无关。我之所以给人留下深刻的印象,是因为在Visual Studio的“运行任务”中,它显示了对命名管道方法的引用,并将其列为活动。
真正的问题是i已将应用程序
shutdownmode
更改为onexplicitsHutdown
,因此,即使关闭主窗口后,除非shutdown()方法被调用。我的修复程序是覆盖
闭合的
事件。The issue didn't have anything to do with named pipes. I was under the impression it did because in Visual Studio's "Running Tasks" it showed a reference to the named pipe methods and listed them as active.
The real issue was the I had changed the application
ShutdownMode
toOnExplicitShutdown
so even after the main window was closed the application would remain running unless theShutdown()
method is called. My fix was to override theOnClosed
event.