Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 从ElapsedEventHandler返回一个值_C# - Fatal编程技术网

C# 从ElapsedEventHandler返回一个值

C# 从ElapsedEventHandler返回一个值,c#,C#,如何从ShowStatus事件返回值?这是我的密码。我想返回是或否的结果 谢谢 Timer1 = new System.Timers.Timer(); Timer1.Elapsed += new ElapsedEventHandler(ShowStatus); Timer1.Interval = 30000; Timer1.Enabled = true; public void ShowStatus(object sourc

如何从ShowStatus事件返回值?这是我的密码。我想返回是或否的结果

谢谢

        Timer1 = new System.Timers.Timer();
        Timer1.Elapsed += new ElapsedEventHandler(ShowStatus);
        Timer1.Interval = 30000;
        Timer1.Enabled = true;

   public void ShowStatus(object source, ElapsedEventArgs evt)
    {
        try
        {
            Timer1.Enabled = false;
            string result;

            IPAddress ip = "127.0.0.1";

            result = FindStatus(ip.ToString()) ? YES: NO;
        }
        Timer1.Enabled = true;
    }

    public bool FindStatus(string ip)
    {
        Ping p = new Ping();
         ingOptions opts = new PingOptions();
            opts.DontFragment = true; 
            PingReply rc = p.Send(ip, 120, buff, opts);
            if (rc.Status.Equals(IPStatus.Success)) return true;
            return false;
    }

无法更改ShowStatus的签名,因为它必须与ElapsedEventHandler委托匹配

必须使用成员变量将当前状态传达给其他方法:

private bool statusFound;

protected void ShowStatus(object source, ElapsedEventArgs evt)
{
    // ...
    statusFound = FindStatus(ip.ToString());
    // ...
}

你想把它退回哪里?是否要使用“是/否”更新UI元素?是。我想更新UI元素。好的。我将使用成员变量。