有没有更好的方法来做到这一点? [英] Is there better way to do this?

查看:72
本文介绍了有没有更好的方法来做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我的编程很新,这是我第一次使用While / Do While。



这是我的代码:

Hey guys, am quite new in programming and this is my first time using "While/Do While".

this is mine code:

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





该代码应告诉我何时与互联网断开连接以及何时互联网再次上升,但我不希望它不断显示msgbox。所以我的问题是如何让我在点击确定之后停止显示msgbox并且有更好的方法吗?



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?

推荐答案

如果我找到你,你应该显示只有当状态发生变化时才会出现一个消息框,即 IsAvailable False 更改为(或相反)。为此,您必须存储最后 IsAvailable 值并将其与当前值进行比较:如果它们相等则不执行任何操作;另一方面,如果它们不同,则显示一个消息框。类似于:

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:
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


您可以订阅前夕与IsAvailable属性相关联。

订阅事件时,每当IsAvailable属性的值发生变化时,您都会收到通知。



你不会需要循环和消耗CPU周期。



你可以这样写:

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:
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





欲了解更多信息(以及原始示例),您可以查看我.Computer.Network.NetworkAvailabilityChanged事件 [ ^ ]。



问候,

Daniele。



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

Regards,
Daniele.


你是混淆的迷吗?





Are you a fan of obfuscation?


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 ) ;
}


这篇关于有没有更好的方法来做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆