private void NodeReceiver_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (e.Cancelled) { ConnNodeBtn.Text = "连接节点"; bConnect = false; Tclient.Close(); Dclient.Close(); AddtoBox(Color.Black, "数据接收取消!\n/>"); } else if (e.Error != null) { ConnNodeBtn.Text = "连接节点"; AddtoBox(Color.Black, "数据接收中断!\n/>"); Dclient.Close(); Tclient.Close(); AddtoBox(Color.Black, "错误信息:" + e.Error.ToString() + "\r\n/>"); } else { ConnNodeBtn.Text = "连接节点"; AddtoBox(Color.Black, "数据接收中断!\n/>"); //AddtoBox(Color.Black, "错误号:" + e.Error.ToString() + "\r\n/>"); Dclient.Close(); Tclient.Close(); } }
private void CommAnsReceiver_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (e.Cancelled) { ConnNodeBtn.Text = "连接节点"; bConnect = false; AddtoBox(Color.Black, "Command ans receive Cancelled!\r\n/>"); } else if (!bConnect) { ConnNodeBtn.Text = "连接节点"; Tclient.Close(); Dclient.Close(); } }
private void NodeLinker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (e.Cancelled) { ConnNodeBtn.Text = "连接节点"; string errstring = "操作已被取消!"; SendStatusLabel(errstring); AddtoBox(Color.Black, errstring + "\r\n"); bConnect = false; Tclient.Close(); Dclient.Close(); } else if (e.Result != null) { ConnNodeBtn.Text = "连接节点"; string errstring = " 操作失败!" + "错误号:" + e.Result.ToString(); SendStatusLabel(errstring); AddtoBox(Color.Black, errstring + "\r\n"); bConnect = false; Tclient.Close(); Dclient.Close(); } else { bConnect = true; ConnNodeBtn.Text = "断开连接"; SendStatusLabel("已连接节点"); AddtoBox(Color.Black, "已连接节点\r\n"); if (Tclient.Connected) { networkdata data = new networkdata(); data.DataType = 0; CommAnsReceiver.RunWorkerAsync(data); } if (Dclient.Connected) { networkdata data = new networkdata(); data.DataType = 1; NodeReceiver.RunWorkerAsync(data); } } }
private void AvailabilityChangedCallback(object sender, EventArgs e) { NetworkAvailabilityEventArgs myEg = (NetworkAvailabilityEventArgs)e; if (!myEg.IsAvailable) { if (Tclient == null || Dclient == null) { return; } if (Tclient.Client != null) { if (Tclient.Connected) { Tstream.Close(); Tclient.Close(); } } else { Tclient.Close(); } if (Dclient.Client != null) { if (Dclient.Connected) { Dstream.Close(); Dclient.Close(); } } else { Dclient.Close(); } if (DLoadDataWorker.IsBusy) { DLoadDataWorker.CancelAsync(); ACPacketHandle.Set(); } MessageBox.Show("网络状态出错,请检查网络!"); bConnect = false; } }
public void ConnNodeBtn_Click(object sender, EventArgs e) { //开始连接 if (ConnNodeBtn.Text == "连接节点") { IPCollections Icf = new IPCollections(); //读取节点 XmlDocument xmlfile = new XmlDocument(); xmlfile.Load(xmldoc); string strip; XmlNode xn = xmlfile.DocumentElement; xn = xn.SelectSingleNode("descendant::节点配置"); foreach (XmlNode subnode in xn.ChildNodes) { string[] ip = { "节点配置", subnode.Name, "网络配置", "节点IP" }; strip = XmlHelper.GetConfigValue(xmldoc, ip); Icf.IPList.Items.Add(subnode.Name + " " + strip); } if (Icf.ShowDialog() == DialogResult.OK) { int g = Icf.IPList.CheckedIndices[0]; string[] str = Icf.IPList.Items[g].ToString().Split(' '); IPAddress Nodeip = IPAddress.Parse(str[1]); ConnectNode(Nodeip); } Icf.Dispose(); } //取消连接 else { bConnect = false; this.NodeLinker.CancelAsync(); this.NodeReceiver.CancelAsync(); this.CommAnsReceiver.CancelAsync(); if (Tclient.Client != null) { if (Tclient.Connected) { Tstream.Close(); Tclient.Close(); } } else { Tclient.Close(); } if (Dclient.Client != null) { if (Dclient.Connected) { Dstream.Close(); Dclient.Close(); } } else { Dclient.Close(); } SendStatusLabel("未连接节点"); ConnNodeBtn.Text = "连接节点"; AddtoBox(Color.Black, "与节点断开\r\n"); bConnect = false; } }