public void ThreadProcess(object state) { while (true) { try { IpHelper helper = new IpHelper(LastIp); Thread thread = new Thread(new ParameterizedThreadStart(helper.GetIp)); thread.Start(); if (!thread.Join(TimeSpan.FromMinutes(1.0))) { _logger.Error("get ip timeout", new object[0]); } string ip = helper.IP; _logger.Info("get ip:{0} - {1}", new object[] { LastIp, ip }); InformationReceived?.Invoke(this, new MessageEventArgs($"{DateTime.Now.ToLongTimeString()}:动态域名获取本机最新IP:{ip}")); if (ip != LastIp) { var ipChangedArgs = new IpChangedEventArgs(LastIp, ip); LastIp = ip; _logger.Info("change ip 1:{0}", new object[] { ip }); IPChanged?.Invoke(this, ipChangedArgs); } } catch (Exception exception) { _logger.Error("ThreadProcess error:{0}-{1}", new object[] { Local, exception }); } finally { Thread.Sleep(TimeSpan.FromMinutes(3.0)); } } }
async void NetAddrChanged(object sender, EventArgs e) { var tmpnow = DateTime.Now; bool AvailabilityChanged = InternetAvailable; await Task.Delay(0).ConfigureAwait(false); // asyncify CheckInet(address_changed: true); AvailabilityChanged = AvailabilityChanged != InternetAvailable; if (InternetAvailable) { // CLEANUP: Console.WriteLine("DEBUG: AddrChange: " + oldV4Address + " -> " + IPv4Address); // CLEANUP: Console.WriteLine("DEBUG: AddrChange: " + oldV6Address + " -> " + IPv6Address); IPAddress oldV6Address = IPv6Address; IPAddress oldV4Address = IPv4Address; InterfaceInitialization(); // Update IPv4Address & IPv6Address bool ipv4changed = false, ipv6changed = false; ipv4changed = !oldV4Address.Equals(IPv4Address); var sbs = new System.Text.StringBuilder(); if (AvailabilityChanged) { Log.Information("<Network> Internet connection restored."); sbs.Append("Internet connection restored!").AppendLine(); } if (ipv4changed) { var outstr4 = new System.Text.StringBuilder(); outstr4.Append("IPv4 address changed: ").Append(oldV4Address).Append(" -> ").Append(IPv4Address); Log.Information(outstr4.ToString()); sbs.Append(outstr4).AppendLine(); } ipv6changed = !oldV6Address.Equals(IPv6Address); if (ipv6changed) { var outstr6 = new System.Text.StringBuilder(); outstr6.Append("IPv6 address changed: ").Append(oldV6Address).Append(" -> ").Append(IPv6Address); Log.Information(outstr6.ToString()); sbs.Append(outstr6).AppendLine(); } if (sbs.Length > 0) { Tray.Tooltip(4000, sbs.ToString(), "Taskmaster", System.Windows.Forms.ToolTipIcon.Warning); // TODO: Make clicking on the tooltip copy new IP to clipboard? } if (ipv6changed || ipv6changed) { IPChanged?.Invoke(this, null); } } else { if (AvailabilityChanged) { Log.Warning("<Network> Unstable connectivity detected."); Tray.Tooltip(2000, "Unstable internet connection detected!", "Taskmaster", System.Windows.Forms.ToolTipIcon.Warning); } } //NetworkChanged(null,null); }