Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, am quite new in programming and this is my first time using "While/Do While".

this is mine code:
VB
If My.Computer.Network.IsAvailable = False Then
           'do nothing
           While My.Computer.Network.IsAvailable = False
               Label1.Text = ("Internet Down")
               Do While My.Computer.Network.IsAvailable = True
                  MsgBox("Internet Back UP")
               Loop
           End While
       End If


That code should tell me when i get disconnected from internet and when internet is up again, but i don't want it to constantly showing msgbox. So my question is how do i make it stop showing msgbox after i click ok and is there better way to do this?
Posted
Comments
PIEBALDconsult 27-Jul-14 16:54pm    
You mean other than using a better language? :D
DeadlyNoodle 27-Jul-14 23:57pm    
I am not sure do you judge my English or you meant to say better programming language, if so i would go back to school if i wanted review of English knowledge. If you meant on better programming language well i am just a beginner..

If I got you, you should display a message box only when a change of state happens, that is whenever IsAvailable changes from False to True (or the opposite). In order to do that, you have to store last IsAvailable value and compare it with the current one: if they are equal then do nothing; on the other hand, if they are different then show a message box. Something similar to:
VB
While True
  if My.Computer.Network.IsAvailable <> lastIsAvailable then
    lastIsAvailable = My.Computer.Network.IsAvailable 'update last IsAvailable value
    if lastIsAvailable then
      MsgBox("Internet Back UP")
    Else
      MsgBox("Internet DOWN")
    End If
  End If
  ' execute other, possibly useful, statements
End While
 
Share this answer
 
v2
Comments
PIEBALDconsult 27-Jul-14 16:52pm    
I don't like the two ifs and two checks of IsAvailable, but at least you eliminated a while.
Sergey Alexandrovich Kryukov 27-Jul-14 17:05pm    
Agree. I also don't like the whole idea of showing a message box repeatedly in a loop. This is OP's idea, but at least it should be noted.
—SA
CPallini 27-Jul-14 17:40pm    
Well, the checks are different.
Sergey Alexandrovich Kryukov 27-Jul-14 17:05pm    
This time, a 4. :-)
—SA
CPallini 27-Jul-14 17:41pm    
Thank you.
You can subscribe to the event associated with the IsAvailable property.
Subscribing the event you would receive notification every time the value of IsAvailable property changes.

You will not need to loop and consume CPU cycles.

You can write something like:
VB
Private Sub DisplayAvailability(ByVal available As Boolean, ByVal notify As Boolean)
    If available Then
        Label1.Text = ("Internet Up")
        If notify Then 
            MsgBox("Internet Back UP")
        End If
    Else
        Label1.Text = ("Internet Down")
        If notify Then 
            MsgBox("Internet DOWN")
        End If
    End If
End Sub 

Private Sub MyComputerNetwork_NetworkAvailabilityChanged( _
    ByVal sender As Object, _
    ByVal e As Devices.NetworkAvailableEventArgs)

    DisplayAvailability(e.IsNetworkAvailable, True)
End Sub 

Private Sub Handle_NetworkAvailabilityChanged()
    DisplayAvailability(My.Computer.Network.IsAvailable, False)

    AddHandler My.Computer.Network.NetworkAvailabilityChanged, _
       AddressOf MyComputerNetwork_NetworkAvailabilityChanged
End Sub

...
...

Private Sub Init()
    Handle_NetworkAvailabilityChanged()
End Sub


For more information (and the original example) you can look at My.Computer.Network.NetworkAvailabilityChanged Event[^].

Regards,
Daniele.
 
Share this answer
 
v3
Are you a fan of obfuscation?


C#
int state = 0 ;

for(;;)
{
    switch ( state = ( ( state << 1 ) | System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable().GetHashCode() ) & 3 )
    {
        case 1 : break ; /* Has become true  */ 
        case 2 : break ; /* Has become false */ 
    }

    System.Threading.Thread.Sleep ( 1000 ) ;
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900