private IPData TestAddress(string hostName) { IPData ipd = new IPData(); try { ipd.CheckTime = DateTime.Now; Ping ping = new Ping(); string address = GetIPAddress(hostName); PingReply replay = ping.Send(address); if (replay.Status == IPStatus.Success) { ipd.ResponseTime = replay.RoundtripTime; ipd.ExternalIP = GetExternalIPAddress(urlDNSIP); ipd.Status = "Good"; } else { ipd.ResponseTime = replay.RoundtripTime; ipd.ExternalIP = GetExternalIPAddress(urlDNSIP); ipd.Status = replay.Status.ToString(); } } catch (Exception ex) { string s = ex.Message; ipd.ResponseTime = -1; ipd.ExternalIP = "0.0.0.0"; ipd.Status = "Err: " + ex.Message; } return(ipd); }
private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; Stopwatch sw = new Stopwatch(); DateTimeOffset dT = DateTimeOffset.Now; StringBuilder sb = new StringBuilder(); IPData ipd = new IPData(); try { while (true) { if (worker.CancellationPending == true) { e.Cancel = true; e.Result = "Cancelled"; break; } else { dT = DateTimeOffset.Now; sw.Start(); ipd = TestAddress(urlTeresoft); sw.Stop(); ipd.ElapsedMilliseconds = sw.ElapsedMilliseconds; count++; worker.ReportProgress(count, ipd); sw.Reset(); if (cycles > 0 && count >= cycles) { e.Result = "Cycle maximum reached"; break; } Thread.Sleep(frequency * 1000); } } } catch (Exception ex) { string s = ex.Message; } }
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { try { DateTimeOffset dT = DateTimeOffset.Now; StringBuilder sb = new StringBuilder(); IPData ipd = new IPData(); TimeSpan ts = new TimeSpan(); ipd = (IPData)e.UserState; textExternalIP.Text = ipd.ExternalIP; if (textExternalIP.Text != previousIP) { if (previousIP != "0.0.0.0") { string[] parts = ipd.ExternalIP.Split('.'); if (parts.Length == 4) //then numbers { countChangedIPs++; } else // Error text { nwErrors++; } textNWErrors.Text = nwErrors.ToString(); textPreviousIP.Text = previousIP; previousIP = ipd.ExternalIP; sb.Clear(); sb.Append(countChangedIPs.ToString()); textIPChangeCount.Text = sb.ToString(); ts = dT - startDT; LastChangeDT = dT; textDownTime.Text = ts.TotalMilliseconds.ToString() + " ms"; if (ts.TotalMilliseconds > maxDowntime) { maxDowntime = ts.TotalMilliseconds; } if (countChangedIPs > 1) { totalDowntime += ts.TotalMilliseconds; } } } else { startDT = dT; ts = dT - startDT; textDownTime.Text = ts.TotalMilliseconds.ToString() + " ms"; } ts = dT - beginningDT; sb.Clear(); sb.Append(ts.Duration().ToString()); sb.Length -= 8; textRunningTime.Text = sb.ToString(); textLastScan.Text = dT.ToString("yyyy-MM-dd HH:mm:ss"); textMaxDownTime.Text = (maxDowntime / 1000).ToString("F2") + " secs";; textResponseTime.Text = ipd.ResponseTime.ToString() + " ms"; textElapsedms.Text = ipd.ElapsedMilliseconds.ToString() + " ms"; textLastChangeTime.Text = LastChangeDT.ToString("yyyy-MM-dd HH:mm:ss"); textTotalDownTime.Text = (totalDowntime / 1000).ToString("F2") + " secs"; sb.Clear(); sb.Append(dT.ToString("yyyy-MM-dd HH:mm:ss")); sb.Append(" - Cycle: ").Append(e.ProgressPercentage.ToString()); sb.Append(", External IP: ").Append(ipd.ExternalIP); sb.Append(", Ping Time: ").Append(ipd.ResponseTime.ToString()).Append(" ms"); sb.Append(", Cycle Time: ").Append(ipd.ElapsedMilliseconds.ToString()).Append(" ms"); sb.AppendLine(); string text = textLogs.Text; sb.Append(text); textLogs.Text = sb.ToString(); textLogs.Refresh(); if (checkSaveFile.Checked == true && textFileLocation.Text != "") { if (sw != null) { sb.Clear(); sb.Append(dT.ToString("yyyy-MM-dd HH:mm:ss")); sb.Append(" External IP: ").Append(ipd.ExternalIP); sb.Append(", Previous IP: ").Append(previousIP); sb.Append(", Failed Duration: ").Append(textDownTime.Text); sb.Append(", Ping Time: ").Append(textResponseTime.Text); sb.Append(", Cycle Time: ").Append(textElapsedms.Text); sw.WriteLine(sb.ToString()); sw.Flush(); } } } catch (Exception ex) { string s = ex.Message; } }