【问题标题】:How to timeout a constructor in C#?如何在 C# 中使构造函数超时?
【发布时间】:2015-10-31 10:00:34
【问题描述】:

我正在使用 IBM Websphere MQ 7.5.0.5,我发现在某些情况下,当服务器端关闭时,QueueManager 的构造函数会挂起。这个构造函数被“使用”包围 子句如以下代码。如何在一定时间后强制终止构造函数?

using (mqQueueManager = new MQQueueManager(this._queueManager, this._properties))

【问题讨论】:

标签: c# .net constructor message-queue ibm-mq


【解决方案1】:

您可以在您的函数中执行类似的操作来实例化 MQQueueManager:

       System.Threading.Thread thread = new System.Threading.Thread(()=>{

        using (mqQueueManager = new MQQueueManager(this._queueManager, this._properties))
        {
        }

        });
        thread.Start();
        var isComplete = thread.Join(TimeSpan.FromSeconds(30));

        if (!isComplete)
        {
            thread.Abort();
        }

【讨论】:

  • 但是一旦线程退出,mqQueueManager 对象就会通过离开 using 子句而被释放,我不能在主线程中使用它。所以我需要将所有逻辑转移到该线程的工作中?
  • 如果你想继续使用 mqQueueManager 那你为什么要处理它?只需创建 mqQueueManager 的新实例,不要丢弃它,然后您可以在主线程中使用该实例。
  • 我试图利用“使用”子句的自动处理。但我认为这很好。如果没有“使用”子句,我可以稍后手动处理。
  • 请务必注意MQ's threading model,否则您可能会发现应用程序仍然阻塞,只是在其他地方。此外,现代 MQ 客户端中的一些阻塞行为在客户端的 ini 文件中作为重新连接参数进行了调整。如果启用了重新连接并且设置了较长的超时时间,您可以尝试减少它。
猜你喜欢
  • 2018-03-26
  • 2016-07-16
  • 2016-11-03
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 2019-04-02
相关资源
最近更新 更多