private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args) { lock (stateObject) { try { if (args.Cancelled || isDisposing) // Must've been a shutdown { return; } if (args.Error != null || !IPAddress.TryParse(args.Result.Trim(), out IPAddress currentIPAddress) || (allowIPv4 && currentIPAddress.AddressFamily != AddressFamily.InterNetwork) || (allowIPv6 && currentIPAddress.AddressFamily != AddressFamily.InterNetworkV6)) { // Something went wrong, try a different URL if (IPFetchURLs.Length > 1) // If we only have one URL, we'll try next time { IncreaseFetchURLIndex(); FetchIPAsync(); } return; } // Everything ok if (!currentIPAddress.Equals(LastIPAddress)) // Check if we have a new IP address { IPAddress oldIPAddress = LastIPAddress; IPAddress newIPAddress = currentIPAddress; LastIPAddress = currentIPAddress; Engine.Dispatcher.InvokeAsync(() => { IPAddressChanged?.Invoke(this, oldIPAddress, newIPAddress); }); } } finally { downloadsPending--; Monitor.PulseAll(stateObject); } } }
private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) { logger.LogInformation("IP address has changed in this computer"); IPAddressChanged?.Invoke(GetIpForAllInterfaces()); }