private void broadcast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (!string.IsNullOrWhiteSpace(tbBroadcastMessage.Text)) { lock (((ICollection)MainWindowVM.OnlineUsers).SyncRoot) { for (int i = 0; i < MainWindowVM.OnlineUsers.Count; i++) { var u = MainWindowVM.OnlineUsers.ElementAtOrDefault(i); if (u != null) { ServerService.InvokeClientService(u, service => service.NotifyMessage(new IMessage { Message = tbBroadcastMessage.Text, MessageKind = 1, Sender = new UserPoint { UserName = "******", OrganizationName = "系统控制中心", UserGuid = Guid.Empty.ToString() }, SendTime = DateTime.Now })); } } } tbBroadcastMessage.Clear(); } }
private void kickoff_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Image img = sender as Image; var user = img.DataContext as UserPoint; lock (((ICollection)MainWindowVM.OnlineUsers).SyncRoot) { MainWindowVM.OnlineUsers.Remove(user); } ServerService.InvokeClientService(user, service => service.KickOff(user.ConvertToBase())); }
public void UserLogin(UserPoint user) { lock (((ICollection)MainWindowVM.OnlineUsers).SyncRoot)//估计:在lock中启用新线程,当lock中的代码执行完毕而新线程还在执行,那么新线程后续代码不再处于lock保护之下 { var users = MainWindowVM.OnlineUsers.Where(o => o.UserID == user.UserID && o.UserGuid != user.UserGuid).ToArray(); if (users.Count() > 0) { Parallel.ForEach(users, u => { MainWindowVM.OnlineUsers.Remove(u); ServerService.InvokeClientService(u, service => service.KickOff(u.ConvertToBase())); }); } } //var orgiUser = MainWindowVM.OnlineUsers.FirstOrDefault(o => o.UserGuid == user.UserGuid); //if (orgiUser == null) //{ OperationContext context = OperationContext.Current; //获取传进的消息属性 MessageProperties properties = context.IncomingMessageProperties; //获取消息发送的远程终结点IP和端口 IPEndPoint endpoint = properties[RemoteEndpointMessageProperty.Name] as IPEndPoint; //RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; user.NetPointAddress = endpoint.ToString();//new IPEndPoint(IPAddress.Parse(endpoint.Address), endpoint.Port); //context.OutgoingMessageProperties.Add("via", user.UDPIMIPPort); lock (((ICollection)MainWindowVM.OnlineUsers).SyncRoot) { MainWindowVM.OnlineUsers.Add(new ServerUserPoint(user) { LoginTime = DateTime.Now }); } //} //else//若存在相同Guid用户则更新,防止客户端之间多次打洞(后注:此处不可能出现这种情况) //{ // lock (((ICollection)MainWindowVM.OnlineUsers).SyncRoot) // { // int index = MainWindowVM.OnlineUsers.IndexOf(orgiUser); // MainWindowVM.OnlineUsers[index] = new ServerUserPoint(user) { LoginTime = DateTime.Now }; // } //} NotifyWhenUserLogin(user); //return new OPResult { IsSucceed = true }; }