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) ...
随机推荐
- IntentFilterDemo
intent-filter示例: <activity android:name=".CustomActivity" android:label="@string/t ...
- (转)Android之自定义适配器
ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果. 有这样一个Demo ...
- 【风马一族_git_github】使用Github搭建个人网站
个人网站 访问 https://用户名.github.io ( 风马一族的Github网站 ) 搭建步骤 1)创建个人站点-->新建仓库(注:仓库名必须是[用户名.github.io]) 2) ...
- 【Sharing】如何成为一名黑客
[声明]此文为转载,只为收藏. 从小到大听说了无数关于“电脑黑客”的故事,比如XXX入侵美国五角大楼,再比如前几年的“熊猫烧香”病毒,这些故事的主角都被我们的媒体称之为“黑客”.其实这些人,更大程度上 ...
- PHP性能分析
内容来自以下站点整理http://jingyan.baidu.com/article/ff4116257e0d5112e48237a0.html关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言 ...
- WindowsMediaPlayer控件批量添加文件至播放列表
思路: 1.读取批定路径的目录文件. 2.用List存放. 3.循环List列表添加到播放列表. public void VidieoPlay() { //WindowsMediaPlayer1.ui ...
- 封装cookie
function cookie(name,value,expires){ switch(typeof value){ case 'string': //设置 var exp=''; if(expire ...
- canvas 绘点图
canvas 绘点图 项目中需要一个记录点实时变动的信息,在此记录一下: <!DOCTYPE html> <html lang="en"> <head ...
- hadoop数据流转过程分析
hadoop:数据流转图(基于hadoop 0.18.3):通过一个最简单的例子来说明hadoop中的数据流转. hadoop:数据流转图(基于hadoop 0.18.3): 这里使用一个例子说明ha ...
- HBase多条件筛选查询方案
最近的项目需要使用Hbase做实时查询,由于Hbase只支持一级索引,也就是使用rowkey作为索引查询,所以对于多条件筛选查询的支持不够,在不建立二级索引的情况下,只能使用Hbase API中提供的 ...