在C# WPF开发中,内存泄漏是一个常见且棘手的问题。内存泄漏会导致应用程序随着时间的推移消耗越来越多的内存,最终可能导致应用程序崩溃或者系统性能下降。以下是一些常见的内存泄漏原因以及相应的排查和解决方法。
1. 事件订阅
在 C# 中,实现两个独立执行程序(EXE)之间的通信是一个常见的需求。这种通信可以通过多种方式实现,包括但不限于命名管道、匿名管道、消息队列、内存映射文件、套接字(Sockets)以及使用 Windows API 如 SendMessage 等。本文将介绍几种常用的方法,并提供示例代码。
1. 使用命名管道进行通信
命名管道是一种常用的进程间通信(IPC)机制,允许不同进程之间进行双向通信。
服务端示例代码
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);
            }
        }
    }
}
客户端示例代码
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 机制,通常用于父子进程之间的通信。
服务端示例代码
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);
        }
    }
}
客户端示例代码
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 程序之间发送消息。
发送端示例代码
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);
    }
}
接收端示例代码
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 则适用于简单的数据传递。开发者可以根据具体需求选择合适的通信方式。
该文章在 2024/10/28 16:33:17 编辑过