public void Search(string searchTerm) { if (repository == null) { throw new InvalidOperationException("You must share the connection with the model before searching."); } if (Group == null) { throw new InvalidOperationException("You must set the group before searching."); } if (String.IsNullOrWhiteSpace(searchTerm)) { return; } if (searchTokenSource != null) { searchTokenSource.Cancel(); } searchTokenSource = new CancellationTokenSource(); searchTask = searchTask.ContinueWith( t => { IsSearching = true; }, searchTokenSource.Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext() ).ContinueWith(t => { var searchResults = repository.GetUsers(searchTerm, includeGroups: true); return(searchResults); }, searchTokenSource.Token).ContinueWith(t => { CancellationToken token = searchTokenSource.Token; SearchResults.Clear(); Group group = repository.GetGroup(Group.Name); var searchResults = t.Result; var models = searchResults.Select(m => userMapper.GetMemberModel(group.DistinguishedName, m)); var modelComparer = KeyComparer <GroupMemberModel> .OrderBy(m => m.FullName).ThenBy(m => m.Name); foreach (var model in models) { token.ThrowIfCancellationRequested(); int index = SearchResults.ToSublist().UpperBound(model, modelComparer); SearchResults.Insert(index, model); } }, searchTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); searchTask.ContinueWith( t => onErrorOccurred(t.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext() ); searchTask.ContinueWith( t => { IsSearching = false; }, searchTokenSource.Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext() ); }
public void RetrieveUsers(string searchTerm) { if (repository == null) { throw new InvalidOperationException("You must connect to AD before querying the users."); } AreGroupsDisplayed = false; AreUsersDisplayed = true; if (tokenSource != null) { tokenSource.Cancel(); } tokenSource = new CancellationTokenSource(); userTask = userTask.ContinueWith( t => { IsSearching = true; }, tokenSource.Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext() ).ContinueWith(t => { CancellationToken token = tokenSource.Token; Users.Clear(); var users = repository.GetUsers(searchTerm); var models = users.Select(u => userMapper.GetModel(u, includeDummy: true)); var modelComparer = KeyComparer <UserModel> .OrderBy(u => u.FullName); foreach (var model in models) { token.ThrowIfCancellationRequested(); int index = Users.ToSublist().UpperBound(model, modelComparer); Users.Insert(index, model); } }, tokenSource.Token); userTask.ContinueWith( t => onErrorOccurred(t.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext() ); userTask.ContinueWith( t => { IsSearching = false; }, tokenSource.Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext() ); }