MSMQ消息队列,一个按钮发送,一个按钮接受
2015-11-15 14:37
337 查看
main 页面
<Window x:Class="MSMQAPP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<Button Width="100" Height="30" Content="发送" Click="btn_Send" Margin="0,10,0,0"/>
<TextBox Width="200" Height="20" x:Name="tb_Mess" Margin="0,10,0,0" Text="张三"/>
<Button Width="100" Height="30" Content="接受" Margin="0,10,0,0" Click="btn_Receive"/>
<TextBox Width="200" Height="200" x:Name="tb_receive" Margin="0,10,0,0" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"/>
</StackPanel>
</Grid>
</Window>
mian.cs 页面
namespace MSMQAPP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DispatcherTimer dtimer = new DispatcherTimer();
private void btn_Send(object sender, RoutedEventArgs e)
{
dtimer.Tick += dtimer_Tick;
dtimer.Interval = new TimeSpan(0,0,5);
dtimer.Start();
}
void dtimer_Tick(object sender, EventArgs e)
{
if (!MessageQueue.Exists(@".\Private$\zhaozlMSMQ"))
{
using (MessageQueue mq = MessageQueue.Create(@".\Private$\zhaozlMSMQ"))
{
mq.Label = "zhaozlPrivateQueue";
mq.Send("MSMQ Private Message", "zhaozl");
}
}
if (MessageQueue.Exists(@".\Private$\zhaozlMSMQ"))//获得私有的消息队列
{
MessageQueue mq = new MessageQueue(@".\Private$\zhaozlMSMQ");
mq.Send(this.tb_Mess.Text + DateTime.Now.ToString(), "zhaozl");//发送消息到私有的消息队列里
this.tb_Mess.Text = string.Empty;
}
}
DispatcherTimer dtime = new DispatcherTimer();
private void btn_Receive(object sender, RoutedEventArgs e)
{
dtime.Tick += dtime_Tick;
dtime.Interval = new TimeSpan(0, 0, 5);
dtime.Start();
}
void dtime_Tick(object sender, EventArgs e)
{
if (MessageQueue.Exists(@".\Private$\zhaozlMSMQ"))//判断私有消息是否存在
{
using (MessageQueue mq = new MessageQueue(@".\Private$\zhaozlMSMQ"))
{
mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//设置消息队列格式化器
MessageEnumerator Me = mq.GetMessageEnumerator2();
while (Me.MoveNext())
{
System.Messaging.Message message = mq.Receive();
this.tb_receive.Text += message.Body + "!";
}
}
}
}
}
}
<Window x:Class="MSMQAPP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<Button Width="100" Height="30" Content="发送" Click="btn_Send" Margin="0,10,0,0"/>
<TextBox Width="200" Height="20" x:Name="tb_Mess" Margin="0,10,0,0" Text="张三"/>
<Button Width="100" Height="30" Content="接受" Margin="0,10,0,0" Click="btn_Receive"/>
<TextBox Width="200" Height="200" x:Name="tb_receive" Margin="0,10,0,0" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"/>
</StackPanel>
</Grid>
</Window>
mian.cs 页面
namespace MSMQAPP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
DispatcherTimer dtimer = new DispatcherTimer();
private void btn_Send(object sender, RoutedEventArgs e)
{
dtimer.Tick += dtimer_Tick;
dtimer.Interval = new TimeSpan(0,0,5);
dtimer.Start();
}
void dtimer_Tick(object sender, EventArgs e)
{
if (!MessageQueue.Exists(@".\Private$\zhaozlMSMQ"))
{
using (MessageQueue mq = MessageQueue.Create(@".\Private$\zhaozlMSMQ"))
{
mq.Label = "zhaozlPrivateQueue";
mq.Send("MSMQ Private Message", "zhaozl");
}
}
if (MessageQueue.Exists(@".\Private$\zhaozlMSMQ"))//获得私有的消息队列
{
MessageQueue mq = new MessageQueue(@".\Private$\zhaozlMSMQ");
mq.Send(this.tb_Mess.Text + DateTime.Now.ToString(), "zhaozl");//发送消息到私有的消息队列里
this.tb_Mess.Text = string.Empty;
}
}
DispatcherTimer dtime = new DispatcherTimer();
private void btn_Receive(object sender, RoutedEventArgs e)
{
dtime.Tick += dtime_Tick;
dtime.Interval = new TimeSpan(0, 0, 5);
dtime.Start();
}
void dtime_Tick(object sender, EventArgs e)
{
if (MessageQueue.Exists(@".\Private$\zhaozlMSMQ"))//判断私有消息是否存在
{
using (MessageQueue mq = new MessageQueue(@".\Private$\zhaozlMSMQ"))
{
mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });//设置消息队列格式化器
MessageEnumerator Me = mq.GetMessageEnumerator2();
while (Me.MoveNext())
{
System.Messaging.Message message = mq.Receive();
this.tb_receive.Text += message.Body + "!";
}
}
}
}
}
}
相关文章推荐
- 批量自动安装常用软件
- 批量自动安装常用软件
- 企业批量部署RealVNC远程控制软件
- 好用的Markdown编辑器一览
- Silverlight将图片转换为byte的实现代码
- 解析Silverlight调用WCF/Rest异常的解决方法
- 网上视频站 视频下载利器 优酷网 视频下载 必备 软件 下载[原创]_常用工具_脚本之家
- 基于Silverlight打印的使用详解,是否为微软的Bug问题
- 子窗口、父窗口和Silverlight之间的相互调用
- BAT批处理实现软件的自动安装(以搜狗拼音为例)
- nw.js实现类似微信的聊天软件
- 采用C#实现软件自动更新的方法
- C#判断某个软件是否已安装实现代码分享
- Silverlight中动态获取Web Service地址
- IIS 防盗链 软件
- silverlight线程与基于事件驱动javascript引擎(实现轨迹回放功能)
- IIS6.0 silverlight网站部署方法
- 浅谈Silverlight 跨线程的使用详解
- asp文件用什么软件编辑