private void button1_Click(object sender, EventArgs e) { button2_Click(sender, e); //redispara = redisparasetting.redisparaName; //服务器连接ip,por,password设置 for (int i = 1; i <= 4; ++i) { //检验输入是否为数字,数字是否介于0~255之间 int tmp = -1; TextBox objText = (TextBox)this.Controls["textBox" + i.ToString()]; if (int.TryParse(objText.Text, out tmp) != true) { MessageBox.Show("错误:服务器IP地址输入错误,请重新输入!", "ERROR"); return; } else if (!(tmp >= 0 && tmp <= 255)) { MessageBox.Show("错误:服务器IP地址输入错误,请重新输入!", "ERROR"); return; } } String IP = textBox1.Text + '.' + textBox2.Text + '.' + textBox3.Text + '.' + textBox4.Text; int port = 0; if (int.TryParse(textBox5.Text, out port) != true) { MessageBox.Show("错误:服务器端口号输入错误,请重新输入!", "ERROR"); return; } serverpara.RedisIP = IP; serverpara.RedisPort = textBox5.Text; serverpara.RedisPassword = textBox6.Text; serverpara.connectvalid = false; //host主机参数 格式“password@ip:port” string[] host = { serverpara.RedisPassword + '@' + serverpara.RedisIP + ':' + serverpara.RedisPort }; try { //从连接池获得只读连接客户端 long initialDB = 0; RedisClient Client = (RedisClient)redismanager.GetReadOnlyClient(ref (initialDB), ref (host)); //byte[] ConnectTimeout = System.BitConverter.GetBytes(3); //Client.ConfigSet("repl-ping-slave-period", ConnectTimeout); if (Client == null || !Client.Ping()) { throw new Exception("连接服务器失败!"); } //连接成功 serverpara.connectvalid = true; redismanager.DisposeClient(ref (Client)); //dispose客户端 Properties.Settings.Default.Save(); // 存储上一次成功连接的IP地址和端口号\ firsttimerun = true; //第一次运行完成 //测试连接 MessageBox.Show(Client.Db.ToString()); //db index MessageBox.Show(Client.DbSize.ToString()); //绘制用户界面 if (machinestate == null) { machinestate = new UserControlMachineState(); } machinestate.Visible = true; machinestate.listView1.Visible = true; machinestate.Dock = DockStyle.Fill; //绘制标题 machinestate.Height = this.panel1.Height; machinestate.Width = this.panel1.Width; if (!machinestate.ListViewTitleDraw(ref (LineNo))) { throw new Exception(); } this.panel1.Controls.Add(machinestate); //机床状态监测画面初始化 ListViewInitial(); //机床状态监测画面刷新 t = new System.Timers.Timer(1000); //实例化Timer类,设置间隔时间为10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(ListViewRefrush); //到达时间的时候执行事件; t.AutoReset = true; //设置是执行一次(false)还是一直执行(true); t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件; } catch (Exception ex) { MessageBox.Show("ERROR:" + ex.Message, "ERROR"); if (machinestate != null) { machinestate.Visible = false; machinestate = null; } redismanager.dispose(); //redispara.dispose(); } }
public void Initialize() { CheckForIllegalCrossThreadCalls = false; //解决线程间的访问限制 //LineNo、LineCount初始化 LineNo = 0; LineCount = 0; string lineindex = global::INDNC.Properties.Settings.Default.LineIndex; string linecount = global::INDNC.Properties.Settings.Default.LineCount; if (lineindex == "" || linecount == "") { lineindex = "生产线路1"; linecount = "1"; } string countstring = ""; if (lineindex != "") { int offset = lineindex.IndexOf("生产线路") + 4; countstring = lineindex.Substring(offset); } try { if (UInt16.TryParse(countstring, out LineNo) != true) { throw new Exception(); } if (UInt16.TryParse(linecount, out LineCount) != true) { throw new Exception(); } } catch (Exception) { MessageBox.Show("ERROR:软件初始化错误,请设置相关参数!", "ERROR"); } //绘制标题 try { if (machinestate.listView1 == null) { machinestate.listView1 = new ListView(); } if (!machinestate.ListViewTitleDraw(ref (LineNo))) { throw new Exception(); } } catch (Exception ex) { MessageBox.Show("ERROR:" + ex.Message, "ERROR"); } //redispara初始化 try { //读取本地Redis服务器参数 /*FileStream fs = new FileStream(@"../LocalRedisPara.conf", FileMode.OpenOrCreate); * StreamReader sr = new StreamReader(fs); * string str = sr.ReadLine(); * //关闭流 * sr.Close(); * fs.Close(); * if (str == null || str == "") * return; * int offsetIP = str.IndexOf("RedisIP="); * int offsetPort = str.IndexOf("RedisPort="); * int offsetPW = str.IndexOf("RedisPassword="******"ERROR:本地服务器参数配置错误!"); * } * * offsetIP += 8; * offsetPort += 10; * offsetPW += 14; * * int i = str.IndexOf(';'); * redispara.RedisIP = str.Substring(offsetIP, i - offsetIP); * i = str.IndexOf(';', i + 1); * redispara.RedisPort = str.Substring(offsetPort, i - offsetPort); * i = str.IndexOf('\0', i + 1); * redispara.RedisPassword = str.Substring(offsetPW); * redispara.connectvalid = true;*/ redispara.RedisIP = global::INDNC.Properties.Settings.Default.localip1 + '.' + global::INDNC.Properties.Settings.Default.localip2 + '.' + global::INDNC.Properties.Settings.Default.localip3 + '.' + global::INDNC.Properties.Settings.Default.localip4; redispara.RedisPort = global::INDNC.Properties.Settings.Default.localport; redispara.RedisPassword = global::INDNC.Properties.Settings.Default.localpassword; redispara.connectvalid = false; } catch (Exception ex) { MessageBox.Show("ERROR:" + ex.Message, "ERROR"); } //serverpara初始化 try { serverpara.RedisIP = global::INDNC.Properties.Settings.Default.ip1 + '.' + global::INDNC.Properties.Settings.Default.ip2 + '.' + global::INDNC.Properties.Settings.Default.ip3 + '.' + global::INDNC.Properties.Settings.Default.ip4; serverpara.RedisPort = global::INDNC.Properties.Settings.Default.port; serverpara.RedisPassword = global::INDNC.Properties.Settings.Default.serverpassword; serverpara.connectvalid = false; } catch (Exception ex) { MessageBox.Show("ERROR:" + ex.Message, "ERROR"); } }