1,105,384 Community Members

Form freezing on exception error

Member Avatar
Minko
Light Poster
29 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

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

        Try

            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,

Minko

Member Avatar
Reverend Jim
Noli mentula
5,450 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
Minko
Light Poster
29 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Minko

Member Avatar
Reverend Jim
Noli mentula
5,450 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

If you have any problems getting the code to work feel free to continue this thread.

Member Avatar
Minko
Light Poster
29 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

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))
        ServerStatusThread.Start()

            Private Delegate Sub dlgUpdateServerStatus(ByVal status As String)

    Public Sub ServerStatus()

        Do While frmLoginVisible
            If ConnectToServer() Then
                UpdateServerStatus("Online")
            Else
                UpdateServerStatus("Offline")
            End If
            System.Threading.Thread.Sleep(5000)
        Loop

    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)
        Else
            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,

Minko

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article