如何关闭名为pipeclementeam?

发布于 2025-02-11 12:52:54 字数 896 浏览 1 评论 0原文

我无法关闭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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2025-02-18 12:52:54

这个问题与命名管道无关。我之所以给人留下深刻的印象,是因为在Visual Studio的“运行任务”中,它显示了对命名管道方法的引用,并将其列为活动。

真正的问题是i已将应用程序shutdownmode更改为onexplicitsHutdown,因此,即使关闭主窗口后,除非shutdown()方法被调用。我的修复程序是覆盖闭合的事件。

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);

    System.Windows.Application.Current.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 to OnExplicitShutdown so even after the main window was closed the application would remain running unless the Shutdown() method is called. My fix was to override the OnClosed event.

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);

    System.Windows.Application.Current.Shutdown();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文