/// <summary> /// 线程获取数据 /// </summary> private static void RecMsg() { while (ifConnect) { // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = 0; try { if (Com.UserInfo.sockClient != null && Com.UserInfo.sockClient.Connected) { length = Com.UserInfo.sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; if (length > 0) { RecMsgdelegate outdelegate = new RecMsgdelegate(Revice); AsyncCallback acb = new AsyncCallback(CallBackRecMsg); IAsyncResult iar = outdelegate.BeginInvoke(arrMsgRec, acb, outdelegate); } } } catch (SocketException se) { Com.UserInfo.sockClient.Dispose(); } } }
private static void CallBackRecMsg(IAsyncResult ar) { //从C#异步调用状态ar.AsyncState中,获取委托对象 RecMsgdelegate dn = (RecMsgdelegate)ar.AsyncState; //一定要EndInvoke,否则你的下场很惨 string Type = dn.EndInvoke(ar); ShowNewMessage(Type); //OnStart(); }