MessageQueue的使用

using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        /***************************************************/
        // Provides an entry point into the application.
        //         
        // This example gets lists of queues by a variety
        // of criteria.
        /***************************************************/

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Send normal and high priority messages.
            myNewQueue.GetQueuesByCategory();
            myNewQueue.GetQueuesByLabel();
            myNewQueue.GetQueuesByComputer();
            myNewQueue.GetAllPublicQueues();
            myNewQueue.GetPublicQueuesByCriteria();
            myNewQueue.GetPrivateQueues();
                        
            return;
        }


        /***************************************************/
        // Gets a list of queues with a specified category.
        // Sends a broadcast message to all queues in that
        // category.
        /***************************************************/
        
        public void GetQueuesByCategory()
        {
            // Get a list of queues with the specified category.
            MessageQueue[] QueueList = 
                MessageQueue.GetPublicQueuesByCategory(new 
                Guid("{00000000-0000-0000-0000-000000000001}"));

            // Send a broadcast message to each queue in the array.
            foreach(MessageQueue queueItem in QueueList)
            {
                queueItem.Send("Broadcast message.");
            }
            
            return;
        }


        /***************************************************/
        // Gets a list of queues with a specified label.
        // Sends a broadcast message to all queues with that
        // label.
        /***************************************************/
        
        public void GetQueuesByLabel()
        {
            // Get a list of queues with the specified label.
            MessageQueue[] QueueList = 
                MessageQueue.GetPublicQueuesByLabel("My Label");

            // Send a broadcast message to each queue in the array.
            foreach(MessageQueue queueItem in QueueList)
            {
                queueItem.Send("Broadcast message.");
            }
            
            return;
        }


        /***************************************************/
        // Gets a list of queues on a specified computer. 
        // Displays the list on screen.
        /***************************************************/
        
        public void GetQueuesByComputer()
        {
            // Get a list of queues on the specified computer.
            MessageQueue[] QueueList = 
                MessageQueue.GetPublicQueuesByMachine("MyComputer");

            // Display the paths of the queues in the list.
            foreach(MessageQueue queueItem in QueueList)
            {
                Console.WriteLine(queueItem.Path);
            }

            return;
        }


        /***************************************************/
        // Gets a list of all public queues.
        /***************************************************/
        
        public void GetAllPublicQueues()
        {
            // Get a list of public queues.
            MessageQueue[] QueueList = 
                MessageQueue.GetPublicQueues();
    
            return;
        }


        /***************************************************/
        // Gets a list of all public queues that match 
        // specified criteria. Displays the list on 
        // screen.
        /***************************************************/
        
        public void GetPublicQueuesByCriteria()
        {
            // Define criteria to filter the queues.
            MessageQueueCriteria myCriteria = new 
                MessageQueueCriteria();
            myCriteria.CreatedAfter = DateTime.Now.Subtract(new 
                TimeSpan(1,0,0,0));
            myCriteria.ModifiedBefore = DateTime.Now;
            myCriteria.MachineName = ".";
            myCriteria.Label = "My Queue";
            
            // Get a list of queues with that criteria.
            MessageQueue[] QueueList = 
                MessageQueue.GetPublicQueues(myCriteria);

            // Display the paths of the queues in the list.
            foreach(MessageQueue queueItem in QueueList)
            {
                Console.WriteLine(queueItem.Path);
            }

            return;
        }


        /***************************************************/
        // Gets a list of private queues on the local 
        // computer. Displays the list on screen.
        /***************************************************/
        
        public void GetPrivateQueues()
        {
            // Get a list of queues with the specified category.
            MessageQueue[] QueueList = 
                MessageQueue.GetPrivateQueuesByMachine(".");

            // Display the paths of the queues in the list.
            foreach(MessageQueue queueItem in QueueList)
            {
                Console.WriteLine(queueItem.Path);
            }
            
            return;
        }
    }
}

 本文引用通告测试:http://blog.661z.com/2010/03/08/messagequeuedeshiyong/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值