locked
How to check a condition always ? RRS feed

  • Question

  • Hi !

    If I want to check a condition always for example checking network status what should I do ?

    Should I use timer ?

    Tuesday, November 19, 2013 6:10 PM

Answers

  • Hello,

    You could use NetworkChange.NetworkAvailabilityChanged Event. You get to this via project properties, application tab, press the button labeled "View Application events". Using the ComboBoxes at the top of the code window select MyApplication Events then NetworkAvailabilityChanged.

    In the example below frmMainForm has a StatusStrip with a image. Dependent on the network status one of two images are shown. I don't expect you to try to exactly match up the sample code but to consider going this route or a similar route.

    Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
       SetConnectionStatus(e.IsNetworkAvailable)
    End Sub
    Public Sub SetConnectionStatus(ByVal connected As Boolean)
       If Not (connected) Then
          frmMainForm.ToolStripStatusLabel1.Image = My.Resources.NetworkDown
       Else
          frmMainForm.ToolStripStatusLabel1.Image = My.Resources.NetWorkUp
       End If
    End Sub
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Tuesday, November 19, 2013 7:04 PM
  • That kind of depends on if the "condition" has its own event handler or not. If not use a timer. If so use the "conditions" event handler.

    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Tuesday, November 19, 2013 9:13 PM

All replies

  • Hi, what do you mean by always ? If it's in regular interval then timer is ok.

    Satya R Biswal

    Tuesday, November 19, 2013 6:51 PM
  • Hello,

    You could use NetworkChange.NetworkAvailabilityChanged Event. You get to this via project properties, application tab, press the button labeled "View Application events". Using the ComboBoxes at the top of the code window select MyApplication Events then NetworkAvailabilityChanged.

    In the example below frmMainForm has a StatusStrip with a image. Dependent on the network status one of two images are shown. I don't expect you to try to exactly match up the sample code but to consider going this route or a similar route.

    Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
       SetConnectionStatus(e.IsNetworkAvailable)
    End Sub
    Public Sub SetConnectionStatus(ByVal connected As Boolean)
       If Not (connected) Then
          frmMainForm.ToolStripStatusLabel1.Image = My.Resources.NetworkDown
       Else
          frmMainForm.ToolStripStatusLabel1.Image = My.Resources.NetWorkUp
       End If
    End Sub
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Tuesday, November 19, 2013 7:04 PM
  • There are three different timers available in Windows Form. If you want to check network status at regular interval  then you can use System.Timers.Timer

    Following article should help you decide the timer you should use based on your requirement.

    http://msdn.microsoft.com/en-us/magazine/cc164015.aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


    Tuesday, November 19, 2013 7:04 PM
  • That kind of depends on if the "condition" has its own event handler or not. If not use a timer. If so use the "conditions" event handler.

    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Tuesday, November 19, 2013 9:13 PM