private void btnSendSerializableObject_Click(object sender, EventArgs e) { try { // 注: 对象序列化发送数据较大 if (studentType != StudentType.None) { // 正在执行 throw new Exception("being implemented"); } IntPtr connId = client.ConnectionId; Thread thread = new Thread(new ThreadStart(delegate() { Student[] students = new Student[2]; students[0] = new Student(); students[0].Id = 1; students[0].Name = "张三"; students[0].Sex = Sex.Female; students[1] = new Student(); students[1].Id = 2; students[1].Name = "李四"; students[1].Sex = Sex.Male; // 发送数组对象 studentType = StudentType.Array; if (client.SendBySerializable(students)) { AddMsg(string.Format("$ ({0}) Send OK --> {1}", connId, "Student[]")); } else { AddMsg(string.Format("$ ({0}) Send Fail --> {1}", connId, "Student[]")); } //////////////////////////////////////////////////////////////////////////////// // 改变性别 students[0].Sex = Sex.Transsexual; List <Student> stuList = new List <Student>(); stuList.Add(students[0]); stuList.Add(students[1]); // 防止粘包,延迟2秒发送下一组数据 AddMsg(" *** 2 seconds after sending ..."); Thread.Sleep(2000); // 发送list对象 studentType = StudentType.List; if (client.SendBySerializable(stuList)) { AddMsg(string.Format("$ ({0}) Send OK --> {1}", connId, "List<Student>")); } else { AddMsg(string.Format("$ ({0}) Send Fail --> {1}", connId, "List<Student>")); } //////////////////////////////////////////////////////////////////////////////// // 防止粘包,延迟2秒发送下一组数据 AddMsg(" *** 2 seconds after sending ..."); Thread.Sleep(2000); // 改变性别 students[0].Sex = Sex.Unknown; // 发送单个学员信息 studentType = StudentType.Single; if (client.SendBySerializable(students[0])) { AddMsg(string.Format("$ ({0}) Send OK --> {1}", connId, "Student")); } else { AddMsg(string.Format("$ ({0}) Send Fail --> {1}", connId, "Student")); } })); thread.Start(); } catch (Exception ex) { studentType = StudentType.None; AddMsg(string.Format("$ Send Fail --> msg ({0})", ex.Message)); } }