【问题标题】:want to read msmq message fast using safe thread in c# .net想在 c# .net 中使用安全线程快速读取 msmq 消息
【发布时间】:2011-07-13 07:12:38
【问题描述】:

我想读取 MSMQ,其中以字节为单位的队列数据和在 1 分钟内生成的队列数约为 1500。因此,如果连续读取,队列 cpu 会持续 30%。过了一段时间,它停止了。我需要大量阅读队列长达 4 小时.. 所以我希望以不应该被阻塞的方式安全地读取线程。 其实我不擅长穿线,所以请你帮帮我..

目前我正在以这种方式阅读

    bool ProcessStatus; //process
    Thread _UDPthreadConsme;

    private void btn_receive_Click(object sender, EventArgs e)
    {

    if (MessageQueue.Exists(@".\private$\myquelocal"))
    {

    ThreadStart _processrcs = new ThreadStart(receivemessages);
    _UDPthreadConsme = new Thread(_processrcs);
    ProcessStatus = true;
    _UDPthreadConsme.Start();
    }
    }


    private void receivemessages()
    {
    MessageBox.Show("Start");
    while (ProcessStatus)
    {
    try
    {

    // Connect to the a queue on the local computer.
    MessageQueue myQueue = new MessageQueue(@".\private$\myquelocal");


    System.Messaging.Message[] myMessagecount = myQueue.GetAllMessages();

    if (myMessagecount.Length <= 0)
    return;


    myQueue.Formatter = new BinaryMessageFormatter();

    // Receive and format the message.
    System.Messaging.Message myMessage = myQueue.Receive();
    byte[] buffer = (byte[])myMessage.Body;

// here i convert buffer to its related structure and then insert the values in database sqlserver.

}
} 

【问题讨论】:

    标签: c# multithreading thread-safety msmq


    【解决方案1】:

    我会这样重写代码

        private void receivemessages()
        {
            Console.WriteLine("Start");
    
            MessageQueue myQueue = new MessageQueue(@".\private$\myquelocal");
    
            while (ProcessStatus)
            {
                try
                {
                    // Waits 100 millisec for a message to appear in queue
                    System.Messaging.Message msg = myQueue.Receive(new TimeSpan(0, 0, 0, 0, 100));
    
                    // Take care of message and insert data into database
    
                }
                catch (MessageQueueException)
                {
                    // Ignore the timeout exception and continue processing the queue
    
                }
            }
        }
    

    【讨论】:

    • 做了一个忍者动作,不小心点击了提交按钮! ;-)
    【解决方案2】:

    这是一个在控制台上运行并异步读取队列的类的示例。这是最安全、最快捷的方法。但是请注意,根据您运行它的位置,如果您正在执行诸如使用消息正文更新文本框之类的操作,您仍然需要某种锁定机制。

    public sealed class ConsoleSurrogate {
    
        MessageQueue _mq = null;
    
        public override void Main(string[] args) {
    
            _mq = new MessageQueue(@".\private$\my_queue", QueueAccessMode.Receive);
            _mq.ReceiveCompleted += new ReceiveCompletedEventHandler(_mq_ReceiveCompleted);
            _mq.Formatter = new ActiveXMessageFormatter();
            MessagePropertyFilter filter = new MessagePropertyFilter();
            filter.Label = true;
            filter.Body = true;
            filter.AppSpecific = true;
            _mq.MessageReadPropertyFilter = filter;
            this.DoReceive();
    
            Console.ReadLine();
            _mq.Close();
        }
    
        void _mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e) {
            _mq.EndReceive(e.AsyncResult);
            Console.WriteLine(e.Message.Body);
            this.DoReceive();
        }
    
        private void DoReceive() {
            _mq.BeginReceive();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2021-06-06
      • 2012-01-07
      • 2012-02-13
      相关资源
      最近更新 更多