Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中检查我的internet是否超时#_C#_Winforms - Fatal编程技术网

C# 如何在C中检查我的internet是否超时#

C# 如何在C中检查我的internet是否超时#,c#,winforms,C#,Winforms,我已经在Visual Studio的Windows窗体应用程序中编程了一个应用程序,并且我已经在我的窗体中设置了一个计时器,该计时器将每隔1秒(1000毫秒)检查用户是否可以访问internet,如果他/她没有访问权限,将弹出一个窗体,显示用户没有访问internet的权限。在我发现即使在互联网显示超时时,表单也会弹出之前,这种方式一直很有效,尽管我想检查用户是否完全无法访问互联网。我决定研究另一种算法,我想让计时器检查互联网是否连续有5次超时,如果有,就会弹出表单,向用户显示他们无法访问互联网

我已经在Visual Studio的Windows窗体应用程序中编程了一个应用程序,并且我已经在我的窗体中设置了一个计时器,该计时器将每隔1秒(1000毫秒)检查用户是否可以访问internet,如果他/她没有访问权限,将弹出一个窗体,显示用户没有访问internet的权限。在我发现即使在互联网显示超时时,表单也会弹出之前,这种方式一直很有效,尽管我想检查用户是否完全无法访问互联网。我决定研究另一种算法,我想让计时器检查互联网是否连续有5次超时,如果有,就会弹出表单,向用户显示他们无法访问互联网。对上述计划有什么具体的想法吗? 这是我的代码,其中显示用户是否可以访问internet。 *LIC是表示用户无法访问互联网的表格* 类中的方法

你可以看到我在计时器上输入的代码

private void TimerCheckInternetConnection_Tick(object sender, EventArgs e)
    {
        #region Check whether the users have internet connection or not
        if (Components.Check_Internet_For_Acceptation() == false)
        {

            if (LIC.Visible)
            {
                LIC.Show();
                return;
            }
            else
                LIC.ShowDialog();
            TimerCheckInternetConnection.Enabled = false;
        }
        #endregion
    }

您可以返回更多状态,而不是返回true和false:

public enum Status
{
     Unknown,
     Connected,
     NoConnection
}

public static int retries = 0;

public static Status Check_Internet_For_Acceptation()
    {
        if (Connections.InternetConnection("google.com") == false)
        {
            if(retries >=5)
               return Status.NoConnection;
            else
               {retries++; return Status.Unknown;}
        }
        else
            return status.Connected;
    }
在计时器中:

private void TimerCheckInternetConnection_Tick(object sender, EventArgs e)
    {
        #region Check whether the users have internet connection or not
        if (Components.Check_Internet_For_Acceptation() == Status.NoConnection)
        {
            // your logic for no connection
        } else if(Components.Check_Internet_For_Acceptation() == Status.Connected)
        {
            //your logic for connected
        }
        //else status is unknown and nothing will happen till it is connected or no connection
            TimerCheckInternetConnection.Enabled = false;
        }
        #endregion
    }

您可以返回更多状态,而不是返回true和false:

public enum Status
{
     Unknown,
     Connected,
     NoConnection
}

public static int retries = 0;

public static Status Check_Internet_For_Acceptation()
    {
        if (Connections.InternetConnection("google.com") == false)
        {
            if(retries >=5)
               return Status.NoConnection;
            else
               {retries++; return Status.Unknown;}
        }
        else
            return status.Connected;
    }
在计时器中:

private void TimerCheckInternetConnection_Tick(object sender, EventArgs e)
    {
        #region Check whether the users have internet connection or not
        if (Components.Check_Internet_For_Acceptation() == Status.NoConnection)
        {
            // your logic for no connection
        } else if(Components.Check_Internet_For_Acceptation() == Status.Connected)
        {
            //your logic for connected
        }
        //else status is unknown and nothing will happen till it is connected or no connection
            TimerCheckInternetConnection.Enabled = false;
        }
        #endregion
    }

这回答了你的问题吗@米凯尔:我相信它终于比我的好了。谢谢你宝贵的帮助help@AliShahbazi检查的逻辑更好,我实际上使用相同的逻辑,但它不会解决检查5次。您可以在我提供的代码中使用此逻辑。感谢你重试了5次。@AshkanMobayenKhiabani万分感谢。我可能会使用您建议的代码。@AliShahbazi非常欢迎您。这是否回答了您的问题@米凯尔:我相信它终于比我的好了。谢谢你宝贵的帮助help@AliShahbazi检查的逻辑更好,我实际上使用相同的逻辑,但它不会解决检查5次。您可以在我提供的代码中使用此逻辑。感谢你重试了5次。@AshkanMobayenKhiabani万分感谢。我可能会使用你建议的代码。@AliShahbazi非常欢迎你。