Ok, 直接上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public class Form1 : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(198, 52);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(143, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Send Message";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(12, 52);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(157, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Received Message";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(151, 94);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(31, 13);
this.label1.TabIndex = 2;
this.label1.Text = "none";
//
// button3
//
this.button3.Location = new System.Drawing.Point(366, 52);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(157, 23);
this.button3.TabIndex = 3;
this.button3.Text = "Stop Receiveing";
this.button3.UseVisualStyleBackColor = true;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 94);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(133, 13);
this.label2.TabIndex = 4;
this.label2.Text = "Received a new message:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(565, 146);
this.Controls.Add(this.label2);
this.Controls.Add(this.button3);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Messaging.MessageQueue mq;
// 如果不存在google这个队列, 那么创建这个队列
if (System.Messaging.MessageQueue.Exists(".\\google"))
{
mq = new System.Messaging.MessageQueue(".\\google");
}
else
mq = System.Messaging.MessageQueue.Create(".\\google");
mq.Purge();// 将队列清空
// 向队列中发送一条消息, 1个随机的数字, 并使用XML序列化进行传输
mq.Send(new System.Messaging.Message(new Random().NextDouble(), new System.Messaging.XmlMessageFormatter()));
}
private void Fn()
{
var mq = new System.Messaging.MessageQueue(".\\google")
{
Formatter = new System.Messaging.XmlMessageFormatter(new[] { typeof(double) })
};
while (true)
{
var message = mq.Receive();
if (null != message)
Invoke(Convert.ToString(message.Body));
Thread.Sleep(100);
}
}
private delegate void Do(string str);
private void Set(string str)
{
label1.Text = str;
}
// 默认情况下, Window form是不允许被非创建它的线程进行访问的, 所以要通过delegate进行安全调用
// 可以通过设置 Control.CheckForIllegalCrossThreadCalls = false; 来取消跨线程调用的限制
private void Invoke(string str)
{
var foo = new Do(Set);
// 可以看到: 是label1 这个控件去调用foo, 是种主动行为. 这样就不会引用跨线程调用异常
label1.Invoke(foo, str);
}
private void button2_Click(object sender, EventArgs e)
{
// 创建一个新的线程去接收队列中的消息
var ts = new ThreadStart(Fn);
var th = new Thread(ts);
MessageBox.Show(@"Start Receiving");
th.Start();
}
}
}
有几个点可以说一下:
1. 首先得有队列, 在没有队列的情况下, new System.Messaging.MessageQueue(“.\\google”) 是会有异常的, 所以得先判断和创建队列通道:
System.Messaging.MessageQueue mq;
// 如果不存在google这个队列, 那么创建这个队列
if (System.Messaging.MessageQueue.Exists(".\\google"))
{
mq = new System.Messaging.MessageQueue(".\\google");
}
else
mq = System.Messaging.MessageQueue.Create(".\\google");
2. window form 默认情况下不可被非创建它的线程进行访问. 这个问题, 简单点的方法是允许它被其它线程访问:
Control.CheckForIllegalCrossThreadCalls = false;
但是这样其实会有一个跨线程访问的风险存在, 是非安全的. 建议的方法是通过delegate的方法, 让控件自己去invoke需要调用的方法, 是一种由被动修改变为主动出击的方式.
