public static void HandleEvent(PresenceChangeEvent e, MainViewModel dataContext) { Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => { if (dataContext.Friends.All(friend => friend.FriendName != e.AccountName)) { dataContext.Friends.Add(new FriendViewModel { FriendName = e.AccountName, Presence = e.PresenceStatus }); } else { var friend = dataContext.Friends.First(f => f.FriendName == e.AccountName); friend.Presence = e.PresenceStatus; } })); }
public void NotifyFriendsOfPresenceChange(Account account, IEnumerable<Account> friends) { Parallel.ForEach(friends, friend => { if (ServiceHelpers.GetPresence(friend) == PresenceStatus.Online) { IEvent evnt = new PresenceChangeEvent { Type = EventType.PresenceChange, Timestamp = DateTime.Now, AccountName = account.UserName, PresenceStatus = ServiceHelpers.GetPresence(account) }; // ReSharper disable once PossibleInvalidOperationException EventManager.SendEventToClient(friend.IpAddress, friend.Port.Value, evnt); } }); }