Solved

Check Internet connected/disconnected status

Posted on 2009-05-06
12
709 Views
Last Modified: 2016-08-29
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
Comment
Question by:imarshad
12 Comments
 
LVL 5

Expert Comment

by:drakeshe
ID: 24322805
Whatever control you are using to access the internet must have a 25 second time out.
0
 
LVL 4

Expert Comment

by:wizzardofoz
ID: 24322852
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
 
LVL 13

Author Comment

by:imarshad
ID: 24322855
Increased point value to 500.....
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24322890
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
 
LVL 13

Author Comment

by:imarshad
ID: 24322979
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24333132
Here is a sample, program...

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

Jack
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24333367
0
 
LVL 13

Author Comment

by:imarshad
ID: 24333821
Thanks for the code..... This code will definitely help me in understanding threadings.....
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24333849
:) Do I get Accepted Answer?
0
 
LVL 13

Author Comment

by:imarshad
ID: 24333992
Yes of course..... Let me just implement this in my own code....
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24370683
Need further assistance?
0
 
LVL 13

Author Comment

by:imarshad
ID: 24371997
I was very busy in another project and  forgot that I had this open..... It is working perfectly....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now