在获得MQMessage时,Backgroundworker完成

时间:2013-11-13 10:20:05

标签: vb.net backgroundworker

我的代码是BackgroundWorkerXMLPutMessages_DoWork Sub。

的一部分
Try
            PublicMQVariable.MQMessage_Reply = New MQMessage
            PublicMQVariable.MQMessage_Reply.CorrelationId = PublicMQVariable.MQMessage_Request.MessageId
            PublicMQVariable.MQMessage_Reply.MessageType = MQC.MQMT_REPLY
            PublicMQVariable.MQGetMessageOptions_Reply = New MQGetMessageOptions
            PublicMQVariable.MQGetMessageOptions_Reply.Options = MQC.MQGMO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING
            PublicMQVariable.MQGetMessageOptions_Reply.MatchOptions = MQC.MQMO_MATCH_CORREL_ID
            PublicMQVariable.MQGetMessageOptions_Reply.WaitInterval = My.Settings.MQ_ReplyTimeOut
            PublicMQVariable.MyStopwatch = New Stopwatch
            PublicMQVariable.MyStopwatch.Start()
            PublicMQVariable.MQQueue_Response.Get(PublicMQVariable.MQMessage_Reply, PublicMQVariable.MQGetMessageOptions_Reply)
            PublicMQVariable.MyStopwatch.Stop()
            PublicMQVariable.MyReplyMessage = PublicMQVariable.MQMessage_Reply.ReadString(PublicMQVariable.MQMessage_Reply.MessageLength)
        Catch ex As MQException
            MsgBox("MQException: compCode: " & ex.CompCode.ToString() & " Reason: " + ex.Reason.ToString() & " - " & ex.Message)
            MQDisconnectAndClose()
            Return
        End Try

执行此代码(PublicMQVariable.MQQueue_Response.Get(PublicMQVariable.MQMessage_Reply, PublicMQVariable.MQGetMessageOptions_Reply))时,它会返回BackgroundWorkerXMLPutMessages_RunWorkerCompleted而不会完成上面的其余代码。

为什么会发生这种情况,我该怎么做才能防止这种“过早完成”?

1 个答案:

答案 0 :(得分:2)

失败并出现异常。忘记在RunWorkerCompleted事件处理程序中检查e.Error是一个非常常见的错误。如果使用Debug + Exceptions,则更容易调试,勾选CLR异常的Thrown复选框。