消息框未从模式对话框窗口中显示
我试图在模式对话框窗口中发生错误时显示消息框,但由于某种原因,尽管我知道 MessageBox.Show 方法被命中,但消息框从未显示。 try catch 块位于显示为模式对话框的窗口窗体的事件处理程序内部。我知道该事件正在被触发并且错误正在被捕获,但消息框从未打开。我已将 MessageBox.Show 替换为另一个表单显示,它工作正常,但我宁愿使用 MessageBox 而不是创建自己的错误表单。我似乎无法让 MessageBox 工作。这是 MessageBox 的限制吗?
这是我正在做的事情的简化版本:
Private Sub OnSomeEvent(ByVal args As MyEventArgs)
Try
'some processing
Catch ex As Exception
ShowMessage("An error has occurred")
End Try
End Sub
Private Delegate Sub _showMessage(ByVal message As String)
Private Sub ShowMessage(ByVal message As String)
If Me.InvokeRequired Then
Me.Invoke(New _showMessage(AddressOf ShowMessage), message)
Else
MessageBox.Show(message, "ERROR")
'also tried MessageBox.Show(Me, message) but no luck
End If
End Sub
*旁注:在这种情况下,我不需要 ShowMessage 的 InvokeRequired 部分,但为了完整性我将其保留(以防万一可能导致问题)。当从后台线程调用它时,我会在其他情况下使用它。在这个特定的实例中,调试时,它会通过 Else 分支。
I am trying to show a message box when an error occurs in a modal dialog window but for some reason the message box is never shown although I know the MessageBox.Show method is being hit. The try catch block is inside of an event handler for a windows form that is being shown as a modal dialog. I know that the event is being fired and that the error is being caught but the message box never opens. I've replaced the MessageBox.Show with another form show and it works fine but I'd rather use MessageBox instead of creating my own error form. I just can't seem to make MessageBox work. Is this a limitation of MessageBox?
Here is a simplified version of what I am doing:
Private Sub OnSomeEvent(ByVal args As MyEventArgs)
Try
'some processing
Catch ex As Exception
ShowMessage("An error has occurred")
End Try
End Sub
Private Delegate Sub _showMessage(ByVal message As String)
Private Sub ShowMessage(ByVal message As String)
If Me.InvokeRequired Then
Me.Invoke(New _showMessage(AddressOf ShowMessage), message)
Else
MessageBox.Show(message, "ERROR")
'also tried MessageBox.Show(Me, message) but no luck
End If
End Sub
*side note: In this case I do not need the InvokeRequired section of ShowMessage but I left it in for completeness (in case somehow that could be causing the issue). I have it there for other cases when it is called from a background thread. In this particular instance when debugging, it goes through the Else branch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查主线程是否被阻止执行其他任务。然后,调用将在
Me.Invoke(New _showMessage(AddressOf ShowMessage), message)
上被阻止,并且您将看不到 MessageBox。如果在
MessageBox.Show(message, "ERROR")
处放置断点,会发生什么情况?Check if the main thread is blocked doing some other task. Then the call will be blocked on the
Me.Invoke(New _showMessage(AddressOf ShowMessage), message)
and you won't see the MessageBox.What happens if you put a breakpoint at the
MessageBox.Show(message, "ERROR")
?