Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check Internet connected/disconnected status

Posted on 2009-05-06
12
Medium Priority
?
782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 15

Accepted Solution

by:
JackOfPH earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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