Threading with Network.Ping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SyGC
    New Member
    • Feb 2008
    • 17

    Threading with Network.Ping

    Hi Guys,

    I have used Threading for Network.Ping to continuously ping an IP address if ping is successful Image A is displayed if not Image B. The code is as follows:

    Code:
    Imports System
    Imports System.Diagnostics
    Imports System.Threading
    
    Public Class Form1
    
        Dim PingThread As Thread
        Dim PRes
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            PingThread = New Thread(AddressOf Me.BackgroundProcess)
            PingThread.Start()
    
        End Sub
    
        Private Sub BackgroundProcess()
    
            Dim IPAddress As String = "192.168.1.121"
    
            Do
                If My.Computer.Network.Ping(IPAddress) = True Then
    
                    PRes = True
                Else
                    PRes = False
                End If
    
                Application.DoEvents()
                Thread.Sleep(1000)
    
            Loop
    
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If PRes = True Then
                PictureBox1.Visible = True
                PictureBox2.Visible = False
            Else
                If PRes = False Then
                    PictureBox1.Visible = False
                    PictureBox2.Visible = True
                End If
            End If
    
    
        End Sub
    
    End Class
    However i have only been able to change the images on a button click. What i want to do is just run my app and for the Images to change automatically without any input. The code below (Not using Threading) allows this to happen, although it is first initiated by a button click.

    Code:
    Imports System
    Imports System.Diagnostics
    
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim IPAddress As String = "192.168.1.121"
    
            Do
    
                If My.Computer.Network.Ping(IPAddress) = True Then
    
                    PictureBox1.Visible = False
                    PictureBox2.Visible = True
                Else
                    PictureBox1.Visible = True
                    PictureBox2.Visible = False
    
                End If
    
                Application.DoEvents()
                System.Threading.Thread.Sleep(10)
    
    
            Loop
        End Sub
    
    End Class
    Ive been looking at my code for hours...i know it must be obvious!!! Thanks in advance guys


    Sy
Working...