On my login form I have a timer and every tick I run this piece of code:


            Client.Connect(IpAddress, Port)

        Catch ex As SocketException

        End Try

and update a label to whether the server is online or offline. However when the server if offline it throws a exception error which makes the form unresponsive for a small amount of time. So my question is, is there a way to stop this?

Any help with this would be much appreciated,


When you try to connect, the connection does not happen instantaneously. The connection timeout has to elapse before the attempt fails. The way to avoid the freezing is to put the code in a separate thread. However, with that approach you have to use a delegate to update your main form from the thread. Fortunately, that is not difficult and I have an example of how to do that here.

Edited 3 Years Ago by Reverend Jim

I thought I was going to need to run it in a new thread, just wanted to make sure it was the correct thing to do.
Thank you very much for the help it is much appreciated.


Edited 3 Years Ago by Minko


I have set it up on another thread but I am having a problem. The problem is it still does not change the text. My code is as follows:

        Dim ServerStatusThread As New Thread(New ThreadStart(AddressOf ServerStatus))

            Private Delegate Sub dlgUpdateServerStatus(ByVal status As String)

    Public Sub ServerStatus()

        Do While frmLoginVisible
            If ConnectToServer() Then
            End If

    End Sub

    Private Sub UpdateServerStatus(ByVal status As String)

        If frmLogin.lblServerStatus.InvokeRequired Then
            Dim dlg As New dlgUpdateServerStatus(AddressOf UpdateServerStatus)
            frmLogin.Invoke(dlg, status)
            frmLogin.lblServerStatus.Text = "Server Status: " & status
        End If

    End Sub

It always runs this line of code:

frmLogin.lblServerStatus.Text = "Server Status: " & status

but the labels text never changes.

Hopefully you can help me figure this out. Thanks for the help so far,


This question has already been answered. Start a new discussion instead.