using System; using System.Threading; using System.Drawing; using System.Windows.Forms; namespace EthernetSwitcher { public class EthernetSwitcherService { private Thread _workerThread; private bool _isRunning; private NotifyIcon _trayIcon; private string _lastSelectedInterface = ""; public void Start(NotifyIcon trayIcon) { _trayIcon = trayIcon; _isRunning = true; _workerThread = new Thread(WorkerLoop); _workerThread.IsBackground = true; _workerThread.Start(); } public void Stop() { _isRunning = false; if (_workerThread != null && _workerThread.IsAlive) { _workerThread.Join(1000); // Wait up to 1 second for thread to finish } } public void UpdateMode(EthernetMode newMode) { // Force immediate execution of the new mode var settings = CurrentSettings.Instance; switch (newMode) { case EthernetMode.Primary: SelectInterface(settings.PrimaryName); UpdateIconFromResource("icon_yellow.ico"); break; case EthernetMode.Secondary: SelectInterface(settings.SecondaryName); UpdateIconFromResource("icon_yellow.ico"); break; case EthernetMode.Auto: HandleAutoMode(settings); break; case EthernetMode.Disabled: UpdateIconFromResource("icon_yellow.ico"); _lastSelectedInterface = "Нет активного интерфейса"; break; } UpdateTrayTooltip(newMode, _lastSelectedInterface); } private void WorkerLoop() { while (_isRunning) { try { var settings = CurrentSettings.Instance; switch (settings.Mode) { case EthernetMode.Primary: SelectInterface(settings.PrimaryName); UpdateIconFromResource("icon_yellow.ico"); break; case EthernetMode.Secondary: SelectInterface(settings.SecondaryName); UpdateIconFromResource("icon_yellow.ico"); break; case EthernetMode.Auto: HandleAutoMode(settings); break; case EthernetMode.Disabled: // Do nothing UpdateIconFromResource("icon_yellow.ico"); _lastSelectedInterface = "Нет активного интерфейса"; break; } // Update tray tooltip after all operations UpdateTrayTooltip(settings.Mode, _lastSelectedInterface); // Wait for the specified interval Thread.Sleep(settings.CheckInterval * 1000); } catch (Exception ex) { Thread.Sleep(5000); // Wait 5 seconds on error before retrying } } } private void UpdateIconFromResource(string resourceName) { try { var icon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream($"EthernetSwitcher.{resourceName}")); if (_trayIcon.Icon?.Handle != icon.Handle) { _trayIcon.Icon = icon; } } catch (Exception ex) { // Fallback to default icon var defaultIcon = Icon.FromHandle(System.Drawing.SystemIcons.WinLogo.Handle); if (_trayIcon.Icon?.Handle != defaultIcon.Handle) { _trayIcon.Icon = defaultIcon; } } } private void HandleAutoMode(Settings settings) { bool primaryLinkExists = CheckInterfaceLink(settings.PrimaryName); // Update tray icon based on link status if (primaryLinkExists) { UpdateIconFromResource("icon_green.ico"); } else { UpdateIconFromResource("icon_red.ico"); } if (primaryLinkExists) { SelectInterface(settings.PrimaryName); DeselectInterface(settings.SecondaryName); } else { SelectInterface(settings.SecondaryName); DeselectInterface(settings.PrimaryName); } } private bool CheckInterfaceLink(string interfaceName) { try { return NetworkManager.GetInterfaceStatus(interfaceName); } catch (Exception ex) { return false; } } private void DeselectInterface(string interfaceName) { if (string.IsNullOrEmpty(interfaceName)) return; try { var settings = CurrentSettings.Instance; if (!NetworkManager.InterfaceExists(interfaceName)) { _lastSelectedInterface = $"Интерфейс не найден: {interfaceName}"; UpdateIconFromResource("icon_red.ico"); return; } switch (settings.Protocol) { case IpProtocol.IPv4: NetworkManager.SetIPv4Status(interfaceName, false); break; case IpProtocol.IPv6: NetworkManager.SetIPv6Status(interfaceName, false); break; case IpProtocol.IPv4_IPv6: NetworkManager.SetIPv4Status(interfaceName, false); NetworkManager.SetIPv6Status(interfaceName, false); break; } } catch (Exception ex) { // Error handling without logging } } private void SelectInterface(string interfaceName) { if (string.IsNullOrEmpty(interfaceName)) return; try { var settings = CurrentSettings.Instance; if (!NetworkManager.InterfaceExists(interfaceName)) { _lastSelectedInterface = $"Интерфейс не найден: {interfaceName}"; UpdateIconFromResource("icon_red.ico"); return; } switch (settings.Protocol) { case IpProtocol.IPv4: NetworkManager.SetIPv4Status(interfaceName, true); break; case IpProtocol.IPv6: NetworkManager.SetIPv6Status(interfaceName, true); break; case IpProtocol.IPv4_IPv6: NetworkManager.SetIPv4Status(interfaceName, true); NetworkManager.SetIPv6Status(interfaceName, true); break; } _lastSelectedInterface = interfaceName; } catch (Exception ex) { // Error handling without logging } } private void UpdateTrayTooltip(EthernetMode mode, string interfaceName) { try { var tooltip = $"Режим: {mode}"; if (!string.IsNullOrEmpty(interfaceName)) { // Shorten interface name to fit in tooltip (Windows 64 char limit) var shortInterfaceName = interfaceName.Length > 25 ? interfaceName.Substring(0, 22) + "..." : interfaceName; tooltip += $"\nИнтерфейс: {shortInterfaceName}"; } if (_trayIcon.Text != tooltip) { _trayIcon.Text = tooltip; } } catch (Exception ex) { // Error handling without logging } } } }