private IEnumerable<Person> StartSearch(SearchInfo searchInfo)
        {
            var token = cancellation.Token;

            // Delay, to allow the user to continue typing.
            Thread.Sleep(2000);

            // A search triggered after this one might have occurred.
            token.ThrowIfCancellationRequested();

            MainWindowViewModel.SearchStatusProperty.SetValue(viewModel, "Searching");

            var query = DataContext.QueryPeople();

            if (!string.IsNullOrEmpty(searchInfo.GivenName))
                query = query.Where(p => p.Firstname.StartsWith(searchInfo.GivenName, StringComparison.CurrentCultureIgnoreCase));

            if (!string.IsNullOrEmpty(searchInfo.FamilyName))
                query = query.Where(p => p.Surname.StartsWith(searchInfo.FamilyName, StringComparison.CurrentCultureIgnoreCase));

            // Represents a delay in retrieving the information.
            Thread.Sleep(1000);

            token.ThrowIfCancellationRequested();

            return query.ToArray();
        }
        public void DoSearch()
        {
            SearchInfo searchInfo = new SearchInfo();
            MainWindowViewModel.SearchInfoProperties.SaveTo(searchInfo, viewModel);

            if (string.IsNullOrEmpty(searchInfo.FamilyName) && string.IsNullOrEmpty(searchInfo.GivenName))
            {
                viewModel.PeopleResults.Clear();
                return;
            }

            // Cancel any previous searches
            cancellation.Cancel();
            cancellation = new CancellationTokenSource();
            searchTask = InitialTask();

            MainWindowViewModel.SearchStatusProperty.SetValue(viewModel, "");

            // Do the search off the UI thread.
            searchTask = searchTask.ContinueWith<IEnumerable<Person>>(
                             previous => StartSearch(searchInfo),
                             cancellation.Token,
                             TaskContinuationOptions.OnlyOnRanToCompletion,
                             TaskScheduler.Default)

            // Update the ViewModel with the search results on the UI thread.
            .ContinueWith(
                             UpdateResults,
                             cancellation.Token,
                             TaskContinuationOptions.OnlyOnRanToCompletion,
                             TaskScheduler.FromCurrentSynchronizationContext());
        }