Share data through MSMQ

firstly. you have to install MSMQ in your computer. Open your add/remove component in your control panel. Select Application Server/Message Queuing, then click OK to finish installing.

In Computer panel you can find Message Queuing node. It demostrates that you have successfully installed the MSMQ. Now let's create a MSMQ for our demo. Right click the Private Queues and select new-->private Queue and named it as MyQueue.

 

secondly, Create two Windows application for the demo. It will realize the Sender and Receiver of the MSMQ.

In the Sender application, you will add a MessageQueue component to the project. It named messageQueue1. Configure it property as follows. The most important two property.
one is Path = ./private$/MyQueue  (. represents the local machine)
the other is Formatter = BinaryMessageFormatter (serilized Formatter)

Another application need the same configure, thus they point to the same MSMQ.

thirdly, write some code about send and receiver.
Sender Application:

private void Sender_Click(object sender, EventArgs e)
{
    System.Collections.ArrayList al = new System.Collections.ArrayList();
    al.Add("Henry");
    al.Add("Tom");
    al.Add("Jenny");
    this.messageQueue1.Send(al, "My first MSMQ");
}

Receiver Application:
private void Receiver_Click(object sender, EventArgs e)
{
    messageQueue1.BeginReceive();
}

private void messageQueue1_ReceiveCompleted(object sender, System.Messaging.ReceiveCompletedEventArgs e)
{
    System.Messaging.Message msg = null;
    msg = messageQueue1.EndReceive(e.AsyncResult);
    System.Collections.ArrayList al = (System.Collections.ArrayList)msg.Body;
    string str = "";
    for (int i = 0; i < al.Count; i++)
    {
        str += al[i].ToString() + "/n";
    }
    MessageBox.Show(str);
}

ok, you can test it. enjoy MSMQ. very easy. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值