c# - 如何在 C# 中检查我的互联网是否超时

标签 c# winforms

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

public static bool Check_Internet_For_Acceptation()
    {
        if (Connections.InternetConnection("google.com") == false)
        {
            return false;
        }
        else
            return true;
    }

你可以看到我在timer_Tick中输入的代码

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
    }

关于c# - 如何在 C# 中检查我的互联网是否超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449333/

相关文章:

c# - 在没有事件源注册的情况下写入 Windows 应用程序事件日志

c# - 拆分器控件(winforms)

c# - 如何调整图片框控件中的图片

c# - VS2010/.NET 4.0 中 BackgroundWorker 线程访问的差异?

c# - 如何在 C# 中使用 Rest Web 服务

c# - 升级到 Android 9 后无法从 Xamarin.Forms 应用程序打开 ESP32 套接字

c# - 按年龄对用户进行分组

c# - 如何判断服务器是否有ssl证书

c# - 从 C# 的 .txt 文件中读取 SQL 查询

html - 使用 vb.net(Winforms) 创建 html 页面并将 css 文件添加到 html 页面