Results 1 to 9 of 9

Thread: Checking Internet Connection constantly

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    7

    Question Checking Internet Connection constantly

    Hay guys.

    I've project where i wanna check if user is connected to internet if he isn't then form must close and another form should show up !

    Problem i'm getting is i can't use check it constantly, even if i use while loop

    Code:
      
    ' keep looping the following code until condition is wrong or until connection is made 
    
            While IsConnectionAvailable() = False
    
                ' code goes here for when connection to hotflask can't be made.
    
                    Form2.Show()
    
    
            End While
    but here form 2 keep showing up and it crashes system, is there any simple way to do it !

    thanks in advance.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Checking Internet Connection constantly

    I don't know specifically about internet connectivity but you can achieve this for general network connectivity (which may be the same thing effectively) by using Application Events. You can access these through the View Application Events button on the project settings page or you can just add your own class :

    Code:
    Namespace My
    
        ' The following events are available for MyApplication:
        ' 
        ' Startup: Raised when the application starts, before the startup form is created.
        ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
        ' UnhandledException: Raised if the application encounters an unhandled exception.
        ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
        ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
        Partial Friend Class MyApplication
    
            Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
                If e.IsNetworkAvailable = False Then
                    Dim MyForm As New Form2
                    MyForm.Show()
                End If
            End Sub
        End Class
    
    End Namespace

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    7

    Re: Checking Internet Connection constantly

    thanks for your effort.

    but it doesn't work !!

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Checking Internet Connection constantly

    When you say it doesn't work? You mean you specifically need to detect when internet connection is down, or it doesn't fire when you unplug/disable your network connection? Or it doesn't compile?

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    7

    Re: Checking Internet Connection constantly

    it doesn't work mean it doesn't work when disable my network, form 2 is not shown.
    when i complie project without internet connection it doesn't work either

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Checking Internet Connection constantly

    How did you add the code - was it by going through the project properties dialog? If not try it that way - it certainly works for me if I disable my wifi connection.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    7

    Question Re: Checking Internet Connection constantly

    lol.. my bad .. i had wierd problem with my wifi. thanks

    i've one more problem, my form takes too long to open and close after network disable/enable.

    here is my code, pls guide me

    ---------------- my app event code ----------------

    vb Code:
    1. Public Function IsConnectionAvailable() As Boolean        ' Returns True if connection is available
    2.  
    3.             ' Replace [url]www.yoursite.com[/url] with a site that
    4.             ' is guaranteed to be online - perhaps your
    5.             ' corporate site, or microsoft.com
    6.             Dim objUrl As New System.Uri("http://www.google.com/")
    7.             ' Setup WebRequest
    8.             Dim objWebReq As System.Net.WebRequest
    9.             objWebReq = System.Net.WebRequest.Create(objUrl)
    10.             Dim objResp As System.Net.WebResponse
    11.             Try
    12.                 ' Attempt to get response and return True
    13.                 objResp = objWebReq.GetResponse
    14.                 objResp.Close()
    15.                 objWebReq = Nothing
    16.                 Return True
    17.             Catch ex As Exception
    18.                 ' Error, exit and return False
    19.  
    20.                 objWebReq = Nothing
    21.                 Return False
    22.             End Try
    23.  
    24.         End Function
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.         Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
    34.             If IsConnectionAvailable() = False Then
    35.  
    36.                 Form2.Show()
    37.                 Form1.Close()
    38.  
    39.             ElseIf IsConnectionAvailable() = True Then
    40.  
    41.                 Form1.Show()
    42.                 Form2.Close()
    43.  
    44.             End If
    45.  
    46.         End Sub

    my startup form is form 2

    ---------------- form 2 code ----------------

    vb Code:
    1. If IsConnectionAvailable() = True Then
    2.             Form1.Show()
    3.             Me.Close()
    4.         End If

    thanks in advance

  8. #8
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Checking Internet Connection constantly

    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Checking Internet Connection constantly

    The thing is, I would say that usually the network connection is not indicative of the internet connection. Many people are behind a router that is almost always connected, but the internet connection to that router might not be available.

    The next best thing is to try and connect to a website like google (or some other website you know will be available).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width