private void btnStart_Click(object sender, EventArgs e) { try { String ip = this.txtIpAddress.Text.Trim(); ushort port = ushort.Parse(this.txtPort.Text.Trim()); // 写在这个位置是上面可能会异常 SetAppState(AppState.Starting); AddMsg(string.Format("$Client Starting ... -> ({0}:{1})", ip, port)); if (client.Connect(ip, port, this.cbxAsyncConn.Checked)) { if (cbxAsyncConn.Checked == false) { SetAppState(AppState.Started); } AddMsg(string.Format("$Client Start OK -> ({0}:{1})", ip, port)); } else { SetAppState(AppState.Stoped); throw new Exception(string.Format("$Client Start Error -> {0}({1})", client.ErrorMessage, client.ErrorCode)); } } catch (Exception ex) { AddMsg(ex.Message); } }
private void buttonStart_Click(object sender, EventArgs e) { try { string ip = textBoxIPAdress.Text.Trim(); ushort port = ushort.Parse(textBoxPort.Text.Trim()); appState = AppState.Starting; //Client组件发起连接的过程可以是同步或异步的 //同步是指组件的连接方法等到连接成功或失败了再返回(返回true或false) //异步是指组件的连接方法会立即返回,如果返回值为false则表示连接失败,如果连接成功则稍后会触发OnConnect事件 if (client.Connect(ip, port, checkBoxAsyncConn.Checked)) { if (checkBoxAsyncConn.Checked == false) { appState = AppState.Started; SetControlState(); } } else { appState = AppState.Stoped; SetControlState(); throw new Exception(string.Format("无法建立连接:{0},{1}", client.ErrorMessage, client.ErrorCode)); } } catch (Exception exc) { ShowMSG(exc.Message); } }
public bool Connect(string ServerIPAddress, ushort Port) { try { if (client.Connect(ServerIPAddress, Port)) { Status = ClientStatus.Running; return(true); } else { Status = ClientStatus.Stopped; throw new Exception(string.Format("客户端启动失败 -> {0}({1})", client.ErrorMessage, client.ErrorCode)); } } catch (Exception ex) { Console.WriteLine("[ERROR]连接失败:" + ex.Message); Status = ClientStatus.Stopped; return(false); } }