How can I return a value from ShowStatus elapsed event? Here is my code. I would like to return result as YES or NO.
Thanks..
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;
}
It’s not possible to alter ShowStatus’ signature since it has to match the
ElapsedEventHandlerdelegate.You’ll have to use a member variable to communicate the current state to other methods: