public void RestartListeners() { if (listeners == null) { return; } StatusDialog d = new StatusDialog(); new Thread(delegate() { d.SetStatus("Restarting listeners..."); EventLogger.LogEvent("Restarting", "Listeners are restarting"); foreach (Listener l in listeners) { l.Stop(); } listeners.Clear(); for (int i = 0; i < Settings.Ports.Count; i++) { Listener l = new Listener(); int port = Settings.Ports[i]; if (l.Listen(port)) { l.SocketAccepted += listener_SocketAccepted; EventLogger.LogEvent("Listening", "Successfully listening on port " + port.ToString()); } else { EventLogger.LogEvent("Listening Failed", "Unable to listen on port " + port.ToString()); } if (l.Running) { listeners.Add(l); } } Invoke((MethodInvoker) delegate { d.Close(); }); }).Start(); d.ShowDialog(); }
void listen() { if (listeners == null) { listeners = new List <Listener>(); Clients = new List <Client>(); foreach (int port in Settings.Ports) { Listener l = new Listener(); if (l.Listen(port)) { l.SocketAccepted += listener_SocketAccepted; EventLogger.LogEvent("Listening", "Successfully listening on port " + port.ToString()); } else { EventLogger.LogEvent("Listening Failed", "Unable to listen on port " + port.ToString()); } if (l.Running) { listeners.Add(l); } } EventLogger.LogEvent("Started", "Net-Weave R has been started"); startToolStripMenuItem.Text = startToolStripMenuItem.Text.Replace("Listen", "Stop"); Pinger.Start(ref Clients); } else { Pinger.Stop(); StatusDialog d = new StatusDialog(); d.SetStatus("Stopping Listeners..."); new Thread(delegate() { foreach (Listener l in listeners) { if (l.Running) { l.SocketAccepted -= listener_SocketAccepted; l.Stop(); EventLogger.LogEvent("Listening Stopped", "The listener for port " + l.Port + " has been stopped"); } } Restart(); d.SetStatus("Disconnecting Clients..."); if (Clients.Count > 0) { for (int i = 0; i < Clients.Count; i++) { Clients[i].DataReceived -= client_DataReceived; Clients[i].DataSent -= client_DataSent; Clients[i].Disconnected -= client_Disconnected; Clients[i].Close(); } } listeners.Clear(); listeners = null; Clients.Clear(); Clients = null; Invoke((MethodInvoker) delegate { EventLogger.LogEvent("Stopped", "Net-Weave R has been stopped"); startToolStripMenuItem.Text = startToolStripMenuItem.Text.Replace("Stop", "Listen"); lstClients.Items.Clear(); d.Close(); }); }).Start(); d.ShowDialog(); } }