Hello.
I am developing an application and i needed for find if a certain IP:Port is online or not and keep updating it. At the moment i have it pinging the IP:Port every time a timer ticks, this works fine but every time it ticks, the application window freezes for a few seconds, which i don't want.
This is the code i have atm:

Imports System.Net
Imports System.Net.Sockets

Public Class MSSUI
    Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
    Dim bBtnClicked As Boolean = False


    Private Sub IPbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IPbox.TextChanged
    End Sub

    Private Sub MSSUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        IPbox.Text = h.AddressList.GetValue(0).ToString + ":27015"

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If bBtnClicked = True Then
            Dim tcpClient As New TcpClient
            Dim IP As String = h.AddressList.GetValue(0).ToString
            Dim Port As Int32 = portbox.Text
            Dim IPAddress As IPAddress = IPAddress.Parse(IP)
            Try
                tcpClient.Connect(IP, Port)
                status1.Text = "Server Online"
            Catch err As Exception
                status1.Text = "No Server Online"
            End Try
        Else
            status1.Text = "No Server Online"
        End If
        bBtnClicked = True

    End Sub
End Class

(The +27015 on *h.AddressList.GetValue(0).ToString + ":27015"* is just to test the port)

Is there a better way to do it than this? all i want to do is find if the IP:Port is online or not and refresh the status. (with out having the application freeze)

If the actual checking works.
Then perhaps you should look into Threading instead of a Timer.

If the actual checking works.
Then perhaps you should look into Threading instead of a Timer.

Thanks for the suggestion. I'm not quite sure how to do that but i can look it up.

There are many methods to do this.
But here's how I do things.

' First add this in a new class file
Imports System.Threading

Public Class DoSomeThreading
   Private m_clsThread As Thread
   Private m_clsNotifyDelegate As NotifyProgress
   Private m_clsSynchronizationObject As System.ComponentModel.ISynchronizeInvoke

   Public Delegate Sub NotifyProgress(ByVal Message As String)

   Public Sub New(ByVal SynchronizationObject As System.ComponentModel.ISynchronizeInvoke, ByVal NotifyDelegate As NotifyProgress)
      m_clsSynchronizationObject = SynchronizationObject
      m_clsNotifyDelegate = NotifyDelegate
   End Sub

   Public Sub Start()
      m_clsThread = New Thread(AddressOf DoWork)
      m_clsThread.IsBackground = True 'Not sure if necessary
      m_clsThread.Start()
   End Sub

   Private Sub DoWork()
      Do While m_clsThread.IsAlive
         'Here you put the code for port scanning
         NotifyUI("status text")
         Thread.Sleep(5000) 'Pause the thread for 5 seconds
      Loop
   End Sub

   Private Sub NotifyUI(ByVal Message As String)
      If m_clsNotifyDelegate IsNot Nothing Then
         Dim args(0) As Object
         args(0) = Message
         m_clsSynchronizationObject.Invoke(m_clsNotifyDelegate, args)
      End If
   End Sub

   'Add some methods or properties to kill the thread.
End Class

' Then in your form
Public Class Form1
   Private Sub someButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles someButton.Click
      Dim threadClass As New DoSomeThreading(Me, New DoSomeThreading.NotifyProgress(AddressOf StatusReport))
      threadClass.Start()
   End Sub

   Private Sub StatusReport(ByVal Message As String)
      lblStatus.Text = Message
   End Sub
End Class

Thanks. Tried it and it worked fine.
I managed to get this which works perfect.

Imports System.Threading

Public Class MSSUI
    Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
    Dim bBtnClicked As Boolean = False
    Dim t As Thread
    Dim Port As Int32

    Private Sub start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click
        Port = portnumberbox.Text
        t = New Thread(AddressOf Me.BackgroundProcess)
        t.Start()
    End Sub

    Private Sub BackgroundProcess()
        Dim i As Integer = 1

        Do While True
            If bBtnClicked = True Then
                Dim tcpClient As New TcpClient
                Dim IP As String = h.AddressList.GetValue(0).ToString
                Dim IPAddress As IPAddress = IPAddress.Parse(IP)
                Try
                    tcpClient.Connect(IP, Port)
                    status1.Text = "Server Online"
                Catch err As Exception
                    status1.Text = "Server Not Online"
                End Try
            Else
                status1.Text = "Server Not Online"
            End If
            bBtnClicked = True
            i += 1
            Thread.Sleep(2000)
        Loop
    End Sub
End Class

That's the entire code that finds if the server is online or not in case anyone is interested. Thanks again for your help Oxiegen.

I'm working on a similar project, I've got everything threaded in a threadpool, run through thousands of IP addresses and it works fine. However I've noticed when the IP is not pingable it is skipped completely; the try/catch isn't grabbing it. I can't seem to find a solution to this, if anyone else has come across this issue before please let me know. Really appreciate it, thanks! -WB

Ok now I feel silly, solution = Catch se as socketexception

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.