public static void parse(string p_id, int com, string msg) { switch (p_id) { case "BS": (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add(msg); })); break; case "SleepTime": Server server = new Server(); server.Sender("BalanceModule", 1, "Спящий режим отключен."); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add("Спящий режим отключен!"); })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).timer_start_scan.Enabled = false; })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).timer_start_scan.Interval = (int.Parse(msg)) * 3600000; })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add("Установлено время запуска через: " + ((Application.OpenForms[0] as Form1).timer_start_scan.Interval) / 3600000 + " часа"); })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).timer_start_scan.Enabled = true; })); server.Sender("BalanceModule", 1, "Спящий режим включен."); server.Sender("BalanceModule", 1, "Деактивация спящего режима через: " + ((Application.OpenForms[0] as Form1).timer_start_scan.Interval) / 3600000 + " часа"); break; } }
private static void Receiver() { Thread th = new Thread(delegate() { Server server = new Server(); server.Sender("BalanceModule", 2, "status"); while (client.Connected) { try { byte[] bytes = new byte[1024]; client.Receive(bytes); MSG packet = new MSG("0", 0, "null"); packet = Util.DeSerialization(bytes); Packages.parse(packet.group, packet.type, packet.message); } catch (SocketException) { Thread.Sleep(5000); continue; } catch (Exception exc) { (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add("[" + DateTime.Now.ToLongTimeString() + "] " + exc.Message); })); } } if (!client.Connected) { try { Thread.CurrentThread.Abort(); } catch { Thread.CurrentThread.Join(); Thread.ResetAbort(); } } //mb this alternative method /* catch (SocketException exc) { if (exc.ErrorCode == 10054 || exc.ErrorCode == 10057) { (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add("Сервер разорвал соединениe.Будет выполнена попытка переподключения..." ); })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.SelectionMode = SelectionMode.One; })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.SetSelected((Application.OpenForms[0] as Form1).listBox1.Items.Count - 1, true); })); (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.SetSelected((Application.OpenForms[0] as Form1).listBox1.Items.Count - 1, false); })); Thread.Sleep(10000); try { Thread.CurrentThread.Abort(); Thread.CurrentThread.Join(); } catch{Receiver();} } } */ }); th.Name = "Приём сообщений"; th.Start(); threads.Add(th); }
private static void Receiver() { Thread th = new Thread(delegate() { Server server = new Server(); server.Sender("BalanceModule", 2, "status"); while (client.Connected) { try { byte[] bytes = new byte[1024]; client.Receive(bytes); MSG packet = new MSG("0", 0, "null"); packet = Util.DeSerialization(bytes); Packages.parse(packet.group, packet.type, packet.message); } catch (SocketException) { Thread.Sleep(5000); continue; } catch (Exception exc) { (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add("[" + DateTime.Now.ToLongTimeString() + "] " + exc.Message); })); } } if (!client.Connected) { try { Thread.CurrentThread.Abort(); } catch { Thread.CurrentThread.Join(); Thread.ResetAbort(); } } //mb this alternative method /* catch (SocketException exc) * { * if (exc.ErrorCode == 10054 || exc.ErrorCode == 10057) * { * (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.Items.Add("Сервер разорвал соединениe.Будет выполнена попытка переподключения..." ); })); * (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.SelectionMode = SelectionMode.One; })); * (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.SetSelected((Application.OpenForms[0] as Form1).listBox1.Items.Count - 1, true); })); * (Application.OpenForms[0] as Form1).listBox1.Invoke((MethodInvoker)(delegate() { (Application.OpenForms[0] as Form1).listBox1.SetSelected((Application.OpenForms[0] as Form1).listBox1.Items.Count - 1, false); })); * * Thread.Sleep(10000); * try * { * Thread.CurrentThread.Abort(); * Thread.CurrentThread.Join(); * } * catch{Receiver();} * } * } */ }); th.Name = "Приём сообщений"; th.Start(); threads.Add(th); }
private void send_msg(string p_id, int com, string msg) { server.Sender(p_id, com, msg); }