Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 摆脱消息队列中的try/catch读取_C#_Try Catch_Message Queue - Fatal编程技术网

C# 摆脱消息队列中的try/catch读取

C# 摆脱消息队列中的try/catch读取,c#,try-catch,message-queue,C#,Try Catch,Message Queue,如何修改以下代码块,使其不依赖于try/catch块和IOTimeout。此函数每1分钟调用一次。所以我只想处理在最后一次调用此函数后排队的Id列表 如果我只是在队列中没有消息等待时收到一个null,那么除了API之外,这就解决了我的问题 显示receive的所有签名都是同步的(我无法阻止线程)或带有MessageQueueException异常,我必须通过try/catch块进入该异常。在阅读了一些建议不要使用try/catch来控制程序流的文章之后,我想避免这种情况 #pragma

如何修改以下代码块,使其不依赖于try/catch块和IOTimeout。此函数每1分钟调用一次。所以我只想处理在最后一次调用此函数后排队的Id列表

如果我只是在队列中没有消息等待时收到一个null,那么除了API之外,这就解决了我的问题 显示receive的所有签名都是同步的(我无法阻止线程)或带有MessageQueueException异常,我必须通过try/catch块进入该异常。在阅读了一些建议不要使用try/catch来控制程序流的文章之后,我想避免这种情况

    #pragma warning disable
    public void ConsumeEvents2()
    {
        var listOfJobs = new List<int>();

        try {
            while (true) { 
                // receive all messages and add in a list to process later
                Message message = messageQueue.Receive(new TimeSpan(0,0,3));
                JobEvent evt = (JobEvent)message.Body;
                listOfJobs.Add(evt.DataNumber);
            }
        } catch (MessageQueueException e) {
            if (e.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout) {
                // process the numbers received
                ProcessJobList(listOfJobs);
                return;
            }
            throw;
        }

    }
    #pragma warning restore
#pragma警告禁用
公共事件2()
{
var listOfJobs=新列表();
试一试{
虽然(正确){
//接收所有消息并添加列表以便稍后处理
Message Message=messageQueue.Receive(新的时间跨度(0,0,3));
JobEvent evt=(JobEvent)message.Body;
添加(evt.DataNumber);
}
}捕获(MessageQueueException){
if(e.MessageQueueErrorCode==MessageQueueErrorCode.IOTimeout){
//处理收到的号码
进程作业列表(作业列表);
返回;
}
投掷;
}
}
#pragma警告恢复

尝试不要直接访问它,请查看microsoft提供的此代码-

using System;
using System.Windows.Controls;
using System.Windows.Messaging;

namespace ReceivingApplication
{
  public partial class Receiver : UserControl
  {
    public Receiver()
    {
        InitializeComponent();

        LocalMessageReceiver messageReceiver =
            new LocalMessageReceiver("receiver",
            ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
        messageReceiver.MessageReceived += messageReceiver_MessageReceived;
        try
        {
            messageReceiver.Listen();
        }
        catch (ListenFailedException)
        {
            output.Text = "Cannot receive messages." + Environment.NewLine +
                "There is already a receiver with the name 'receiver'.";
        }
    }

    private void messageReceiver_MessageReceived(
        object sender, MessageReceivedEventArgs e)
    {
        e.Response = "response to " + e.Message;
        output.Text =
            "Message: " + e.Message + Environment.NewLine +
            "NameScope: " + e.NameScope + Environment.NewLine +
            "ReceiverName: " + e.ReceiverName + Environment.NewLine +
            "SenderDomain: " + e.SenderDomain + Environment.NewLine +
            "Response: " + e.Response;
    }
  }
}

在Windows窗体应用程序中,当在应用程序中的任何位置(在主线程上或在异步调用期间)引发异常时,可以通过在应用程序上注册ThreadException事件来捕获它。通过这种方式,您可以避免为所有方法添加try/catch

Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}

用于ASP.NET应用程序

要在页面中创建全局处理程序,请为 System.Web.UI.TemplateControl.Error事件。创建 应用程序范围的错误处理程序,在Global.asax文件中,将代码添加到 这件事。这些方法称为 如果页面或应用程序中的任何位置发生未处理的异常, 分别地您可以从中获取有关最近错误的信息 GetLastError方法

下面是一些有趣的线索


对于代码的其他部分中可能引发的其他异常,该怎么办?这难道不能排除所有其他例外吗?有没有办法从处理程序中重新抛出异常,使其看起来像是从原始位置抛出的?@Ryan S:此方法捕获所有异常。您可以从这里识别异常类型(例如:ListenFailedException),并从单个位置进行适当的处理。是的,重新引发的异常是从原始位置引发的。这是一个web应用程序,但并不重要。您仍在应用程序级别捕获异常