Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 804
  • Last Modified:

Check Internet connected/disconnected status

Hi all,
I am trying to get the internet (Connected or disconnected) status in my VB.NET application. I am trying the following simple function which is running fine.
    Private Function CheckInternetStatus() As Boolean
        Try
            Dim IPhost As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("www.google.com.pk")
            Return True
        Catch
            Return False
        End Try
    End Function
 
I am calling this function from a Timer after every 30 seconds to check the status of internet connected or disconnected.

Dim InternetStatus As Boolean = CheckInternetStatus()
            If InternetStatus = True Then   'Internet is connected

            Else   'Internet is disconnected

            EndIf
The problem is that whenever I am disconnected the function takes a few seconds(almost 25 seconds) to return False and during this time the User Interface of my application freezes. How can I resolve this freezing of my User Interface? I think creating a separate thread for this might be the way to go but I am unable to find a good solution.
So my requirements are
1) Get status of internet
2) Get the internet downtime
3) Notified when specific time(say 2 min.) has passed and I have not connected to the internet.
(I have implemented all of this functionality using the function above and a timer control but I want it using threading)
0
imarshad
Asked:
imarshad
1 Solution
 
drakesheCommented:
Whatever control you are using to access the internet must have a 25 second time out.
0
 
wizzardofozCommented:
You have two options. Reduce the timeout of the check or create a new thread. The more correct solution would be to create a new thread to handle the connection test. Lag times on an internet connection can be on the order of one second for a badly congested network. A UI that stalls for that long is not good form.

So your thread would be updating connection status every 30 second and using some form of inter-thread communication to send that to your main thread. The main thread only looks at the last known communication status to make its decision.
0
 
imarshadAuthor Commented:
Increased point value to 500.....
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JackOfPHCommented:
Private pingDeviceThread As System.Threading.Thread

'Put this in your timer
Private Sub Timer_Tick()
pingDeviceThread = New System.Threading.Thread(New Threading.ThreadStart(AddressOf PingDevice))
pingDeviceThread.Priority = Threading.ThreadPriority.Lowest
pingDeviceThread.Start()

End Sub


Public Sub PingDevice()
        Dim pinging As New System.Net.NetworkInformation.Ping

           Dim pingResult As Boolean = True
            System.Threading.Thread.Sleep(100)
            Try
                If pinging.Send(System.Net.Dns.GetHostEntry("www.google.com.pk"), 50).Status = Net.NetworkInformation.IPStatus.Success Then
                    pingResult = True
                Else
                    pingResult = False
                End If
            Catch ex As Exception

            End Try

            pingDeviceThread.Abort()
       

    End Sub
0
 
imarshadAuthor Commented:
JackOfPH,
              How will I know that Internet connection is disabled/enabled in my UI? Will I need to use some global variables to hold the Connection status? and also how will I know how much time has passed since I am not connected to internet?
0
 
JackOfPHCommented:
Here is a sample, program...

It is not yet perfect, but this will give you some idea...

Jack
0
 
JackOfPHCommented:
0
 
imarshadAuthor Commented:
Thanks for the code..... This code will definitely help me in understanding threadings.....
0
 
JackOfPHCommented:
:) Do I get Accepted Answer?
0
 
imarshadAuthor Commented:
Yes of course..... Let me just implement this in my own code....
0
 
JackOfPHCommented:
Need further assistance?
0
 
imarshadAuthor Commented:
I was very busy in another project and  forgot that I had this open..... It is working perfectly....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now