/// <summary> /// 运行时预加载项 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form_Main_Load(object sender, EventArgs e) { serverIP = IPAddress.Parse(Registry.ReadKey4Registry("PublishClient", "ServerIP")); uid = Registry.ReadKey4Registry("PublishClient", "CurrentUserID"); idNumeric = int.Parse(uid); uid = idNumeric.ToString("D6"); uac = Registry.ReadKey4Registry("PublishClient", "CurrentUserAccount"); ucl = Registry.ReadKey4Registry("PublishClient", "CurrentUserName"); upw = Registry.ReadKey4Registry("PublishClient", "CurrentUserPW"); // 设置窗口属性 this.Text = "教材补助经费评估软件 [" + ucl + "]" + " [#" + uid + "]"; this.label_ClientName.Text = "[#" + uid + "] " + ucl; // 准备 TCP 监听 tcpServerGet = new TcpListenerP(new IPEndPoint(IPAddress.Any, Port.TCP_BOOK_INFORMATION_PORT)); tcpServerGet.OnThreadTaskRequest += new TcpListenerP.ThreadTaskRequest(OnListenBookInfo); // 准备数据集 BookList = new BookInformationList(); BookEval = new BookEvaluaionList(); // 初始化列表显示 ResetListView_Books(); }
public void OnListenBookInfo(object sender, EventArgs e) { TcpClient tcpClient = sender as TcpClient; int threadID = Port.TCP_BOOK_INFORMATION_PORT % 10000; Console.WriteLine("On Listen..."); using (NetworkStreamP buf = new NetworkStreamP(tcpClient.GetStream())) { buf.ReceiveBufferSize = tcpClient.ReceiveBufferSize; while (true) { try { byte[] raw; buf.Read(out raw); MemoryStream ms = new MemoryStream(raw); BinaryFormatter bf = new BinaryFormatter(); BookInformationList another = bf.Deserialize(ms) as BookInformationList; Invoke(new AddBookInfoList_dele(AddBookInfoList), new object[] { another }); } catch (Exception ex) { Type type = ex.GetType(); if (type == typeof(TimeoutException)) { // 超时异常,不中断连接 Console.WriteLine("{0} [host {1}]: 数据超时失败!", DateTime.Now, threadID); } else { // 仍旧抛出异常,中断连接 Console.WriteLine("{0} [host {1}]: 中断连接异常原因:{2}!", DateTime.Now, threadID, type.Name); throw ex; } } } } }
private void AddBookInfoList(BookInformationList val) { BookList.MergeWith(val); RefreshAllBookList(); }