private void button2_Click(object sender, EventArgs e) { this.button1.SetPropertry <bool>("Enabled", true); this.button2.SetPropertry <bool>("Enabled", false); try { service.Stop(); } catch (Exception ex) { Console.WriteLine(ex); } }
private static void ConsoleStart() { //ServerHost=test.evfort.cn;ServerPort=9201;RemotePort=9203;LocalHost=127.0.0.1;LocalPort=5005; List <ConnectionServerService> services = new List <ConnectionServerService>(); for (int i = 0; i < 100; i++) { string key = string.Format("item{0}", i); string temp = System.Configuration.ConfigurationManager.AppSettings.Get("item" + i.ToString()); if (string.IsNullOrEmpty(temp)) { break; } try { //ServerHost = 远程服务器地址; //ServerPort = 服务器通讯端口; //RemotePort = 外网映射端口; //LocalHost = 局域网主机地址; //LocalPort = 局域网主机端口; string ServerHost = ""; int ServerPort = 0; int RemotePort = 0; string LocalHost = ""; int LocalPort = 0; ProtocolTypes ProtocolType = ProtocolTypes.TCP; temp.ToLower().Split(';').ToList().ForEach((str) => { string[] items = str.Split('='); if (items.Length > 1) { string k = items[0]; string v = items[1]; switch (k) { case "serverhost": ServerHost = v; break; case "serverport": ServerPort = v.ToInt32(); break; case "remoteport": RemotePort = v.ToInt32(); break; case "localhost": LocalHost = v; break; case "localport": LocalPort = v.ToInt32(); break; case "protocoltype": if ("2".Equals(v)) { ProtocolType = ProtocolTypes.HTTP; } break; default: break; } } }); ConnectionServerService service = new ConnectionServerService() { ServerHost = ServerHost, ServerPort = ServerPort, RemotePort = RemotePort, LocalHost = LocalHost, LocalPort = LocalPort, ProtocolType = ProtocolType }; services.Add(service); service.Start(); } catch (Exception ex) { Console.WriteLine("启动失败,请检查配置信息:{0}", temp); } } Console.WriteLine("服务启动成功,按任意键结束!"); Console.ReadLine(); services.ForEach((service) => { service.Stop(); }); }