什么是更好的方法在多线程服务听? [英] What is better approach to listen in MultiThreading Service?

查看:149
本文介绍了什么是更好的方法在多线程服务听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我既MSMQ和线程相对较新的.NET。我要创建在不同的线程负责侦听服务,通过TCP和SNMP,多个网络设备,而这一切东西,在专门的线程中运行,但这里还需要从其他应用程序的MSMQ队列听。 我分析了另一个类似的项目,并有用于下一个逻辑:

I am relatively new both to MSMQ and Threading in .NET. I have to create a service which listen in different threads, via TCP and SNMP, several network Devices and all this stuff run in dedicated threads, but here also is required to listen on MSMQ Queue from another applications. I am analyzing another similar projects and there is used next logic:

private void MSMQRetrievalProc()
{
    try
    {
        Message mes;
        WaitHandle[] handles = new WaitHandle[1] { exitEvent };
        while (!exitEvent.WaitOne(0, false))
        {
            try
            {
                mes = MyQueue.Receive(new TimeSpan(0, 0, 1));
                HandleMessage(mes);
            }
            catch (MessageQueueException)
            {
            }
        }
    }
    catch (Exception Ex)
    {
        //Handle Ex
    }
}

MSMQRetrievalThread = new Thread(MSMQRetrievalProc);
MSMQRetrievalThread.Start();

但在另一个服务(消息分派器)我用异步消息的阅读基础上的 MSDN例的:

public RootClass() //constructor of Main Class
{
    MyQ = CreateQ(@".\Private$\MyQ"); //Get or create MSMQ Queue

    // Add an event handler for the ReceiveCompleted event.
    MyQ.ReceiveCompleted += new
ReceiveCompletedEventHandler(MsgReceiveCompleted);
    // Begin the asynchronous receive operation.
    MyQ.BeginReceive();
}

private void MsgReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult)
{

    try
    {
        // Connect to the queue.
        MessageQueue mq = (MessageQueue)source;
        // End the asynchronous Receive operation.
        Message m = mq.EndReceive(asyncResult.AsyncResult);

        // Process received message

        // Restart the asynchronous Receive operation.
        mq.BeginReceive();
    }
    catch (MessageQueueException Ex)
    {
        // Handle sources of MessageQueueException.
    }
    return;
}

是否异步处理假设每封邮件将在其他处理比主线程? 可以而且需要这个(第二)的方式放在单独的线程?

Does asynchronous handling suppose that every message will be handled in other than main thread? Could and need this (2nd) approach be put in separate thread?

请指点更好的方法或一些简单的替代品。

Please advice better approach or some simple alternatives.

邮件到达队列不具有某种规则定义的行为。这可能是长时间没有拒绝消息将到达或在一秒钟内有我的到达许多(高达10或甚至更多)的消息。根据一些消息,就需要删除定义的动作/改变其运行的线程一些对象。

Messages arrival in Queue doesn't have some rule-defined behavior. It may be that for long time no nay message will arrive or in one second there my arrive many (up to 10 or even more) messages. Based on actions defined in some message it will need to delete/change some objects having running threads.

推荐答案

我强烈建议使用WCF的MSMQ。

I highly recommend using WCF for MSMQ.

http://msdn.microsoft.com/en-us/library/ ms789048.aspx

这允许你异步处理使用WCF线程模型允许节流,封盖,重试,来电​​等...

This allows you to both asynchronous handle the incoming calls using the WCF threading model which allows for throttling, capping, retries, etc...

这篇关于什么是更好的方法在多线程服务听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆