WPF: Debugging Two WPF Applications Communicating via Named Pipes in VS2019

Jane Jie Chen 466 Reputation points
2024-12-08T20:29:18.2233333+00:00

Our application is target WPF and .NET 4.8.

We have two WPF applications. One is Console application which show instrument run status, and another application is a simulator to simulate instrument run actions and status. Both applications communicate through Named Pipes. When the simulator simulates an error, it will generate an event log and send instrument error status to console application and console application will display this instrument error. Sometimes, the console application did not receive this instrument error. We need to debug those two WPF applications.

How do we debug two WPF applications through two VS2019? Thx!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,007 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,804 questions
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 3,645 Reputation points Microsoft Vendor
    2024-12-09T03:43:44.4233333+00:00

    Hi,@Jane Jie Chen. Welcome to Microsoft Q&A. 

    You could try to debug using the following method.

    The test code is as follows: Server-side WPF program—WPF App(.NET Framework 4.8)

    NamedPipeServer.cs

        public class NamedPipeServer
        {
            public async Task StartServer()
            {
                    using (var server = new NamedPipeServerStream("TestPipe", PipeDirection.InOut))
                    {
                        server.WaitForConnection();
    
                        using (var reader = new StreamReader(server))
                        using (var writer = new StreamWriter(server) { AutoFlush = true })
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                // Processing received data
                                Console.WriteLine("Received: " + line);
                                // Send Response
                                await writer.WriteLineAsync("Echo: " + line);
                            }
                        }
                    }         
            } 
        }
    

    MainWindow.xaml.cs

        public partial class MainWindow : Window
        {
            NamedPipeServer namedPipeServer = new NamedPipeServer();     
            public MainWindow()
            {
                InitializeComponent();
                Task.Run(async () => {await namedPipeServer.StartServer(); });
            }
        }
    

    Client-side WPF program—WPF App(.NET Framework 4.8)

    NamedPipeClient.cs

        public class NamedPipeClient
        {
            public async Task StartClient()
            {
                using (var client = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut))
                {
                    client.Connect(); 
                    using (var reader = new StreamReader(client)) 
                    using (var writer = new StreamWriter(client) { AutoFlush = true })
                    {
                        // Sending Data
                        await writer.WriteLineAsync("Hello from client");
                        // Receiving Response
                        string response = await reader.ReadLineAsync(); 
                        Console.WriteLine("Response: " + response); 
                    } 
                } 
            } 
        }
    

    MainWindow.xaml.cs

        public partial class MainWindow : Window
        {
            NamedPipeClient namedPipeClient = new NamedPipeClient();
            public MainWindow()
            {
                InitializeComponent();
                Task.Run(async () => { await namedPipeClient.StartClient(); });
            }
        }
    

    Debugging steps:

    1. Place the above two WPF programs in the same computer.
    2. Use two Visual Studios to open the two WPF programs respectively (I used Visual Studio 2022 for testing, and the 2019 version should not be much different. If debugging still fails, you could upgrade the version of Visual Studio).
    3. Click the following locations to set breakpoints. 1

    2

    1. Run the program in debug mode, first run the Server-side WPF program, then run the Client-side WPF program, you will see that breakpoints have been entered on both sides.

    45

    1. In the client WPF program, click Step Over each time to run the program line by line.

    3

    When it reaches await writer.WriteLineAsync("Hello from client");, the program will wait for the response of the server WPF program. In the server WPF program, when Step Over runs to while ((line = reader.ReadLine()) != null), the client WPF program could continue to run through Step Over. When it runs to string response = await reader.ReadLineAsync();, it will wait for the input of the server again. The same operation will not be repeated here.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.