Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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#_Performance_Time_Timer_Network Connection - Fatal编程技术网

C# 用C语言测量internet连接丢失时间#

C# 用C语言测量internet连接丢失时间#,c#,performance,time,timer,network-connection,C#,Performance,Time,Timer,Network Connection,我想检查我的应用程序运行时是否丢失了internet连接,并测量任何丢失的持续时间。为此,我使用以下代码: try { using (var client = new WebClient()) using (var stream = client.OpenRead("http://www.google.com")) { //Doing something when connec

我想检查我的应用程序运行时是否丢失了internet连接,并测量任何丢失的持续时间。为此,我使用以下代码:

try
        {
            using (var client = new WebClient())
            using (var stream = client.OpenRead("http://www.google.com"))
            {
                //Doing something when connection running
            }
        }
        catch
        {
            //Doing something when connection lost
        }

是否有人知道如何使用此代码编写日志文件来测量我的连接丢失时间(格式:天:分钟:秒)?

您可以使用

System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
如果您想测量时间损失,那么可以在项目中包含计时器控制

timer1.Enabled=true;
timer1.Interval=1000;
int count=0;

private void timer1_tick(object sender,Eventargs e)
{
    if(System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()==false)
    {
        count++;
    }
}

此计数将返回以秒为单位的时间损失。

您试图实现什么目标?在你付出太多的努力来创建一个代码执行这样的事情,你可以考虑我吗?我知道这是一个Delphi问题,但我的答案更符合逻辑,而不是语法。事实上,我想知道,如果连接丢失,我的应用程序有多少时间没有internet连接。为此,我将创建一个日志文件来存储连接丢失时间。如果应用程序的目的是直接记录internet连接的事实,那么您的方法当然没有错。在任何其他情况下,如果网络操作失败,您应该记录,而不是轮询internet连接。是的,我的应用程序直接连接到internet。是否有方法计算连接丢失时间并将其存储到日志文件中?我在找密码。提前感谢!这个很好用。但是我想将计数转换为时间格式。MSDN说“如果任何网络接口被标记为“向上”,而不是环回或隧道接口,则认为网络连接可用。”这对您合适吗?你想检查网络连接还是互联网连接?@UserPhalcon答案是“此计数将以秒为单位返回时间损失。”您还希望使用什么时间格式?@mg30rg我想检查我的互联网连接,并希望输出格式为day:hr:min:sec@UserPhalcon像
TimeSpan timeValue=TimeSpan.FromSeconds(计数)