WPF 进程间通讯----inter-process communication
进程间通讯--inter-process communication
进程间相互通讯的方法有很多,如用web services,xml 等互相读取, 网络的可以使用socket 等.
2个WinForm程序相互通讯可以使用重写WndProc的方法,而WPF则不能。
先看如图效果:
首先新建一个空白解决方案IPC
新建一个WPF项目命名为AppA
我们只需要点击AppA中的button后AppB会提示已经点击即可,
项目A的窗体XAML代码:
1 2 3 4 5 6 7 8 | <Window x:Class= "IPC.AppA.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" Title= "App A" Height= "350" Width= "525" > <Grid> <Button Name= "btnOK" Content= "Button" HorizontalAlignment= "Left" Margin= "202,135,0,0" VerticalAlignment= "Top" Width= "75" /> </Grid> </Window> |
项目A的后置代码:
public partial class MainWindow : Window { [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern uint RegisterWindowMessage(string lpString); private string msgstr = "inter-process communtcation"; private uint msg; private const int HWND_BROADCAST = 0xffff; public MainWindow() { InitializeComponent(); this.btnOK.Click += (s, e) => { this.Dispatcher.Invoke(delegate { PostMessages(); }); }; } protected void PostMessages() { msg = RegisterWindowMessage(msgstr); if (msg == 0) { MessageBox.Show(Marshal.GetLastWin32Error().ToString()); } PostMessage(HWND_BROADCAST, msg, IntPtr.Zero, IntPtr.Zero); }
如下是项目B的窗体代码:
1 2 3 4 5 6 7 8 9 10 | <Window x:Class= "IPC.AppB.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" Title= "App B" Height= "350" Width= "525" > <Grid> <Button Name= "btnOK" Content= "0" HorizontalAlignment= "Left" Margin= "230,132,0,0" VerticalAlignment= "Top" Width= "75" /> </Grid> </Window> |
项目B的后置代码:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace IPC.AppB { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private MainWindow _instance; private object _lock = new object(); private string msgtext = "inter-process communtcation"; private uint msg; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern uint RegisterWindowMessage(string lpString); public MainWindow() { InitializeComponent(); this.Loaded += (s, e) => { Load(); }; //this.btnOK.Click += (s, e) => { // MessageBox.Show("AppB's button is clicked."); //}; } public MainWindow Instance { get { lock (_lock) { if (_instance == null) { _instance = new MainWindow(); } return _instance; } } } protected void Load() { MainWindow main = Instance; main.Dispatcher.Invoke(delegate { msg = RegisterWindowMessage(msgtext); if (msg == 0) { MessageBox.Show(Marshal.GetLastWin32Error().ToString()); } }); } int i = 0; protected void PromptMsgs() { this.Dispatcher.Invoke(new Action(delegate { btnOK.Click += (s, e) => { //do nothing.. }; this.btnOK.Content = (++i).ToString(); MessageBox.Show("AppB's button is clicked."); })); } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource sorce = PresentationSource.FromVisual(this) as HwndSource; sorce.AddHook(new HwndSourceHook(WndProc)); } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == this.msg) { PromptMsgs(); } return IntPtr.Zero; } protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { base.OnClosing(e); Environment.Exit(0); } } }
需要代码的朋友请留言!
作者:风清扬 No.1
出处:http://www.cnblogs.com/fengqingyangNo1
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下右下角的 【关注 风清扬 No.1】。
因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客.
出处:http://www.cnblogs.com/fengqingyangNo1
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下右下角的 【关注 风清扬 No.1】。
因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库