C# 两个exe程序之间通信

科技   2024-10-08 07:32   中国  

在C# WPF开发中,内存泄漏是一个常见且棘手的问题。内存泄漏会导致应用程序随着时间的推移消耗越来越多的内存,最终可能导致应用程序崩溃或者系统性能下降。以下是一些常见的内存泄漏原因以及相应的排查和解决方法。

1. 事件订阅



在 C# 中,实现两个独立执行程序(EXE)之间的通信是一个常见的需求。这种通信可以通过多种方式实现,包括但不限于命名管道、匿名管道、消息队列、内存映射文件、套接字(Sockets)以及使用 Windows API 如 SendMessage 等。本文将介绍几种常用的方法,并提供示例代码。

1. 使用命名管道进行通信

命名管道是一种常用的进程间通信(IPC)机制,允许不同进程之间进行双向通信。

服务端示例代码

csharp

using System;
using System.IO.Pipes;
using System.Text;

public class NamedPipeServer
{
public static void Main()
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut))
{
Console
.WriteLine("Waiting for client connection...");
pipeServer
.WaitForConnection();

using (var reader = new StreamReader(pipeServer))
using (var writer = new StreamWriter(pipeServer))
{
writer
.WriteLine("Hello from server");
writer
.Flush();
string message = reader.ReadLine();
Console
.WriteLine("Received from client: " + message);
}
}
}
}

客户端示例代码

csharp

using System;
using System.IO.Pipes;
using System.Text;

public class NamedPipeClient
{
public static void Main()
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut))
{
Console
.WriteLine("Connecting to server...");
pipeClient
.Connect();

using (var reader = new StreamReader(pipeClient))
using (var writer = new StreamWriter(pipeClient))
{
string message = reader.ReadLine();
Console
.WriteLine("Received from server: " + message);
writer
.WriteLine("Hello from client");
writer
.Flush();
}
}
}
}

2. 使用匿名管道进行通信

匿名管道是另一种 IPC 机制,通常用于父子进程之间的通信。

服务端示例代码

csharp

using System;
using System.IO.Pipes;

public class AnonymousPipeServer
{
public static void Main()
{
using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out))
{
Console
.WriteLine("Writing to client...");
pipeServer
.Write(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0, 4);
}
}
}

客户端示例代码

csharp

using System;
using System.IO.Pipes;

public class AnonymousPipeClient
{
public static void Main()
{
using (AnonymousPipeClientStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In))
{
AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, pipeClient);
pipeServer
.DisposeLocalCopyOfClientHandle();
byte[] buffer = new byte[4];
int bytesRead = pipeClient.Read(buffer, 0, 4);
Console
.WriteLine("Read {0} bytes from server.", bytesRead);
}
}
}

3. 使用 SendMessage 实现通信

SendMessage 是一个 Windows API 函数,可以用来在两个独立的 EXE 程序之间发送消息。

发送端示例代码

csharp

using System;
using System.Runtime.InteropServices;

public class SendMessageSender
{
public const int WM_COPYDATA = 0x004A;

[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);

public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}

public static void Main()
{
string message = "Hello from Sender";
COPYDATASTRUCT cds;
cds
.dwData = IntPtr.Zero;
cds
.cbData = message.Length + 1;
cds
.lpData = message;

int hwnd = FindWindow(null, "Receiver Window Title");
SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
}
}

接收端示例代码

csharp

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class SendMessageReceiver : Form
{
public const int WM_COPYDATA = 0x004A;

public SendMessageReceiver()
{
// Set up the main window...
}

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COPYDATA)
{
COPYDATASTRUCT cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
string text = Marshal.PtrToStringAnsi(cds.lpData, cds.cbData - 1);
MessageBox
.Show(text);
}
base.WndProc(ref m);
}

public static void Main()
{
Application
.Run(new SendMessageReceiver());
}
}

结论

以上介绍了几种在 C# 中实现两个 EXE 程序之间通信的方法。每种方法都有其适用场景和优缺点。命名管道适用于需要全双工通信的场景,匿名管道适用于父子进程间的通信,而 SendMessage 则适用于简单的数据传递。开发者可以根据具体需求选择合适的通信方式。

往期精品推荐:

在国内默默无闻的.NET,在国外火的超乎想象?

C#的膨胀之路:创新还是灭亡

介绍.NET 6款好看的winform开源UI库

介绍一款最受欢迎的.NET 开源UI库

WPF第三方开源UI框架:打造独特体验的魔法师

WPF与Winform,你的选择是?

WinForm的前世今生

.NET成年了,然后呢?——编程界的逆袭传奇


CSharp编程大全
C#编程.net core开发,winform桌面开发,wpf开发,c sharp编程大全,CSharp程序开发,C#开发实例(附源代码),编程过程遇到的各种坑详解!
 最新文章