Solved

Check Internet connected/disconnected status

Posted on 2009-05-06
12
713 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

10 Experts available now in Live!

Get 1:1 Help Now