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:
- Place the above two WPF programs in the same computer.
- 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).
- Click the following locations to set breakpoints.
- 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.
- In the client WPF program, click
Step Over
each time to run the program line by line.
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.