Message processing in asp.net (MSMQ) Three

Source: Internet
Author: User
Tags message queue thread msmq

In the first two articles of this article, the knowledge of MSMQ is introduced, and many friends who read the first two articles have asked questions like this:

1. How to apply MSMQ to the actual project?

2. Can you introduce an actual application example?

3 、......

In the first two articles, about the common technical point of MSMQ is finished, this article mainly in MS Open source project PetShop in the application of MSMQ as a case to introduce the application of MSMQ in the actual project. In PetShop, because the system uses the multi-threaded specialized application to monitor the message queue, before entering the PetShop application analysis, we first come to understand the relevant knowledge point about multithreading and MSMQ.

I. Multithreading and MSMQ

Now there is a requirement that, regardless of the message data in the specified message queue, we monitor the queue through a multi-line thread, but the data in the queue is changed to make corresponding processing, such as reading the message. According to this demand, let's do an example of using a multiple thread to monitor the queue, read the message if there is a message in the queue, and if not, keep the queue monitored and processed (read the message) when the queue data changes (a new message is added).

First, define a thread array to store the number of threads;

1static private int threadnumber = 5; 5-line program
2static private thread[] Threadarray = new Thread[threadnumber];

We load the thread that needs to be started into the Threadarray array and start the thread through an array of traversal, actually there are only 5 threads.

1private void Button1_Click (object sender, EventArgs e)
2{
3 startthreads ();
4}
5
6private void Startthreads ()
7{
8 int counter;//Thread Count
9 for (counter = 0; counter < THREADNU Mber; counter++)
= Threadarray[counter] = new Thread (new ThreadStart (Msmqlisten));
Threadarray[counter]. Start ();
This.richTextBox2.Text + = (counter + 1). ToString () + "line start!" ";
17private void Msmqlisten ()
18{
(True)
{
21//Take out the message in the queue 
  22 MessageBox.Show (Msgqueue.receivemessage ());
24}

Related Article

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.