user10962741
user10962741

Reputation:

Why does MessageBox.Show sometimes fail?

I have some C# and Visual Basic code including this function:

Public Function InitL(ByVal portNrL As Integer) As Output
    Try
        If ComPortL.IsOpen = True Then
            ComPortL.Close()
        End If

        ComPortL.PortName = "Com" & portNrL

        If ComPortL.IsOpen = False Then
            ComPortL.Open()
        End If

        Return New Output(Output.ResultEnum.Successful, "")
    Catch ex As Exception
        Return New Output(Output.ResultEnum.Unsuccessful, ex.Message)
    End Try
End Function

I use it in another class like this:

Output result = x.InitL(Int32.Parse(portnumber));
if (result.Result == Output.ResultEnum.Unsuccessful)
{
    MessageBox.Show(result.ErrorMessage);
    enableAllButtons();
    return;
}

When I choose a busy com port number my program hangs. When I put in a break point and check it, it shows the message box and afterwards works fine every time, until I choose a busy com port number again, at which point the program intermittently hangs.

This is the Output class:

Public Class Output
    Public Result As ResultEnum
    Public ErrorMessage As String

    Public Enum ResultEnum
        Successful = 1
        Unsuccessful = 2
    End Enum

    Public Sub New(_result As ResultEnum, _errorMessage As String)
        Result = _result
        ErrorMessage = _errorMessage
    End Sub
End Class

Upvotes: 0

Views: 198

Answers (0)

Related Questions