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}