WPF 进程间通讯----inter-process communication
进程间通讯--inter-process communication
进程间相互通讯的方法有很多,如用web services,xml 等互相读取, 网络的可以使用socket 等.
2个WinForm程序相互通讯可以使用重写WndProc的方法,而WPF则不能。
先看如图效果:

首先新建一个空白解决方案IPC
新建一个WPF项目命名为AppA
我们只需要点击AppA中的button后AppB会提示已经点击即可,
项目A的窗体XAML代码:
<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 == )
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
}
PostMessage(HWND_BROADCAST, msg, IntPtr.Zero, IntPtr.Zero);
}
如下是项目B的窗体代码:
<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 == )
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
}
});
} int i = ;
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();
}
}
}
需要代码的朋友请留言!
WPF 进程间通讯----inter-process communication的更多相关文章
- wpf进程间通讯
wpf进程间通讯 在联想智能识别项目中,需要用到进程间通讯,并且是低权限向高权限发送消息.首先声明一下,此项目是wpf的. 首先先简要说一下什么时候会用到进程间通讯,如:在Windows程序中,各个进 ...
- Android(java)学习笔记232:Android进程间通讯(IPC)之AIDL
一.IPC inter process communication 进程间通讯 二.AIDL android interface defination language 安卓接口定义语言 满 ...
- Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL
一.IPC inter process communication 进程间通讯 二.AIDL android interface defination language 安卓接口定义语言 满 ...
- Python 第八篇:异常处理、Socket语法、SocketServer实现多并发、进程和线程、线程锁、GIL、Event、信号量、进程间通讯
本节内容: 异常处理.Socket语法.SocketServer实现多并发.进程和线程.线程锁.GIL.Event.信号量.进程间通讯.生产者消费者模型.队列Queue.multiprocess实例 ...
- Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身
本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...
- High Performance Networking in Google Chrome 进程间通讯(IPC) 多进程资源加载
小结: 1. 小文件存储于一个文件中: 在内部,磁盘缓存(disk cache)实现了它自己的一组数据结构, 它们被存储在一个单独的缓存目录里.其中有索引文件(在浏览器启动时加载到内存中),数据文件( ...
- Android进阶笔记04:Android进程间通讯(IPC)之Messenger
一. Android进程间通讯之Messenger 的引入 (1)引言: 平时一说进程间通讯,大家都会想到AIDL,其实messenger和AIDL作用一样,都可以进行进程间通讯.它是基于消 ...
- 服务 远程服务 AIDL 进程间通讯 IPC
Activity aidl接口文件 package com.bqt.aidlservice; interface IBinderInterface { /* 更改文件后缀为[.aidl]去掉 ...
- C#进程间通讯技术-整理。
原文:C#进程间通讯技术-整理. 扩展阅读:http://www.cnblogs.com/joye-shen/archive/2012/06/16/2551864.html 一.进程间通讯的方式 1) ...
随机推荐
- HTML页面中常见的一些小方法
在<Head>标签中加 <meta http-equiv="pragma " content="no-cache"> <met ...
- 【CSS3】---曲线阴影翘边阴影
效果图 代码 index <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title& ...
- 十个顶级的C语言资源助你成为优秀的程序员
译者言:学习C语言,需要一点一滴,沉下心来,找个安静的地方,泡上一杯咖啡,在浓郁的香味中一起品味她.(by Boatman Yang) 人们通常认为计算机编程很烦,但是有些人却从中发现了乐趣.每一个程 ...
- 原生js实现addClass,removeClass,hasClass方法
function hasClass(elem, cls) { cls = cls || ''; if (cls.replace(/\s/g, '').length == 0) return false ...
- 9款完美体验的HTML5/jQuery应用
1.jQuery动画图标菜单导航 仿苹果样式 这次要分享的这款jQuery插件非常酷,它是一款带有动画按钮的jQuery菜单插件.而且从菜单的外观上来看,有点苹果菜单风格的味道.当我们将鼠标滑过菜单项 ...
- C++ Priority Queues(优先队列) and C++ Queues(队列)
C++优先队列类似队列, 但是在这个数据结构中的元素按照一定的断言排列有序. empty() 如果优先队列为空,则返回真 pop() 删除第一个元素 push() 加入一个元素 size() 返回优先 ...
- javascript面向对象--自定义类型
Javascript是基于原型实现面向对象的,因此并没有类和接口,它的对象也与其他基于类的语言中的对象有所不同.在Javascript中,每个对象都是基于一个引用类型创建的,这个引用类型可以是原生类型 ...
- ASP.NET MVC 及 Areas 简单控制路由
ASP.NET MVC中怎么去控制路由,这个想关的文章很多,我在这里就是自我总结一下,仅供参考. 1.我们新建一个项目,查看RouteConfig.cs,代码如下: public static voi ...
- linux解压zip、bz、bz2、z、gz、tar(解包)
zip: 压缩: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>] ...
- jQuery EasyUI 数据网格 - 启用行内编辑(转自http://www.runoob.com/jeasyui/jeasyui-datagrid-datagrid12.html)
可编辑的功能是最近添加到数据网格(datagrid)的.它可以使用户添加一个新行到数据网格(datagrid).用户也可以更新一个或多个行.本教程向您展示如何创建一个数据网格(datagrid)和内联 ...