-
Nov 22nd, 2009, 10:19 AM
#1
Thread Starter
New Member
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.
-
Nov 22nd, 2009, 10:38 AM
#2
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
-
Nov 22nd, 2009, 10:44 AM
#3
Thread Starter
New Member
Re: Checking Internet Connection constantly
thanks for your effort.
but it doesn't work !!
-
Nov 22nd, 2009, 10:46 AM
#4
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?
-
Nov 22nd, 2009, 10:48 AM
#5
Thread Starter
New Member
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
-
Nov 22nd, 2009, 10:51 AM
#6
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.
-
Nov 22nd, 2009, 12:10 PM
#7
Thread Starter
New Member
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:
Public Function IsConnectionAvailable() As Boolean ' Returns True if connection is available
' Replace [url]www.yoursite.com[/url] with a site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("http://www.google.com/")
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
objWebReq = Nothing
Return False
End Try
End Function
Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
If IsConnectionAvailable() = False Then
Form2.Show()
Form1.Close()
ElseIf IsConnectionAvailable() = True Then
Form1.Show()
Form2.Close()
End If
End Sub
my startup form is form 2
---------------- form 2 code ----------------
vb Code:
If IsConnectionAvailable() = True Then
Form1.Show()
Me.Close()
End If
thanks in advance
-
Nov 22nd, 2009, 07:09 PM
#8
Fanatic Member
Re: Checking Internet Connection constantly
-
Nov 22nd, 2009, 09:02 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|