. Net

Source: Internet
Author: User

When a message is used to transmit data, the data volume is usually small. In my projects, the biggest advantage of using it is that you don't have to consider whether the other party is running. As long as the other party goes online, it will probably receive it. In a project, it is used to notify a message when generating large data, and receive the message from the Web service after the other party receives the message. The Code is as follows:

Sender:

1 messageModel. transfersModel tmodel = new messageModel. transfersModel (sourcetext. Trim (), itype, content. Trim ());
2 using (MessageQueue queue = new MessageQueue ("FormatName: DIRECT = tcp:" + address. Trim () + @ "\ Private $ \ message "))
3 {
4 MessageQueueTransaction transMes = new MessageQueueTransaction ();
5 try
6 {
7 transMes. Begin ();
8 queue. Send (tmodel, "transferMessage: {" + sourceip. Trim () + "}");
9 transMes. Commit ();
10}
11 catch
12 {
13 transMes. Abort ();
14}
15}
16

Acceptor:

1 private Thread receiveMessageThread;
2
3 private void Form_Load (object sender, EventArgs e)
4 {
5/*** // receives the message
6 this. queue = new MessageQueue (@ ". \ Private $ \ tgatemessage ");
7 System. Type [] types = new Type [] {typeof (messageModel. transfersModel )};
8 queue. Formatter = new XmlMessageFormatter (types );
9
10 this. receiveMessageThread = new Thread (new ThreadStart (this. getMessage ));
11 this. receiveMessageThread. IsBackground = true;
12 this. receiveMessageThread. Start ();
13}
14
15
16
17 receive messages # region receives messages
18 private MessageQueue queue;
19/** // <summary>
20 // retrieve the message
21 /// </summary>
22 private void getMessage ()
23 {
24 while (true)
25 {
26 MessageEnumerator messageEnum = queue. GetMessageEnumerator2 ();
27 {
28 while (messageEnum. MoveNext ())
29 {
30 System. Messaging. Message message = queue. PeekById (messageEnum. Current. Id );
31 messageModel. transfersModel tmodel = message. Body as messageModel. transfersModel;
32 if (tmodel! = Null)
33 {
34 // retrieve and save the data
35 // call the method to save the data. If the data is saved normally, the corresponding message data is cleared.
36 // if (new dataFromService (). saveData (tmodel. Mestype, takeData ))
37 message = queue. ReceiveById (messageEnum. Current. Id); // Delete
38}
39}
40}
41 Thread. Sleep (1000 );
42}
43}
44 # endregion
45
46

The receiving end uses a separate thread to monitor the message, and the reading of the message itself is blocked. Therefore, when there is no new message, the thread is stopped and runs only when there is a new message, it is blocked again after the operation is complete. The sender and receiver share a data class, and the content can be defined as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.