简体   繁体   English

在MultiThreading Service中听什么更好的方法?

[英]What is better approach to listen in MultiThreading Service?

I am relatively new both to MSMQ and Threading in .NET. 我对MSMQ和.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. 我必须创建一个服务,通过TCP和SNMP,几个网络设备以及所有这些东西在专用线程中运行,在不同的线程中监听,但是这里也需要从另一个应用程序监听MSMQ队列。 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();

But in another service (message dispatcher) I used asynchronous messages' reading based on MSDN Example : 但是在另一个服务(消息调度程序)中,我使用基于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. 请建议更好的方法或一些简单的选择。

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. 可能是很长一段时间没有任何消息将会到达,或者在一秒钟内我会收到许多(最多10个甚至更多)消息。 Based on actions defined in some message it will need to delete/change some objects having running threads. 根据某些消息中定义的操作,它将需要删除/更改一些具有正在运行的线程的对象。

I highly recommend using WCF for MSMQ. 我强烈建议使用WCF进行MSMQ。

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

This allows you to both asynchronous handle the incoming calls using the WCF threading model which allows for throttling, capping, retries, etc... 这允许您使用WCF线程模型异步处理传入调用,该模型允许限制,上限,重试等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM