Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:19:02+00:00 2026-06-03T16:19:02+00:00

NOTE: Depends on this quetion I have a view-model like this: public class ViewModel

  • 0

NOTE: Depends on this quetion

I have a view-model like this:

public class ViewModel {
    private readonly IPersonService _personService;
    private readonly ObservableCollection<SearchPersonModel> _foundedList;
    private readonly DispatcherTimer _timer;
    private readonly Dispatcher _dispatcher;
    private CancellationTokenSource _tokenSource;

    public SearchPatientViewModel(IPersonService personService) {
        _personService = personService;
        _foundedList = new ObservableCollection<SearchPersonModel>();
        _dispatcher = (/*CurrentApplication*/).Dispatcher;
        _timer = new DispatcherTimer(
            TimeSpan.FromMilliseconds(1000),
            DispatcherPriority.Normal,
            TimerCallBack,
            _dispatcher);
        _tokenSource = new CancellationTokenSource();
    }

    public string Term {
        get { return _term; }
        set {
            // implementing INotifyPropertyChanged
            if(_term== value)
                return;
            _term= value;
            OnPropertyChanged(() => Term);
            tokenSource.Cancel(); // canceling prev search query
            _timer.Stop(); // stop the timer to reset it
            // start it again to do a search query if user change not the term for 1000ms
            _timer.Start(); 
        }
    }

    private void TimerCallBack(object sender, EventArgs e) {
        _timer.Stop();
        _tokenSource = new CancellationTokenSource();
        var task = Task<IEnumerable<SearchPersonModel>>.Factory
            .StartNew(Search, _tokenSource.Token);
        _dispatcher.BeginInvoke((Action)(() => {
            _foundedList.Clear();
            foreach(var item in task.Result)
                _foundedList.Add(item);
        }), DispatcherPriority.Background);
    }

    private IEnumerable<SearchPersonModel> Search() {
        return _personService.DoSearch(this.Term);
    }

}

and in the IPersonService implementation I do this:

public class PersonService : IPersonService {
    public IEnumerable<SearchPersonModel> DoSearch(string term){
        System.Threading.Thread.Sleep(10000);
        return some-search-result;
    }
}

However, I expect that while search query is executing, GUI be free. But it froze! Have you any idea where is my mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:19:03+00:00Added an answer on June 3, 2026 at 4:19 pm

    The problem is that evaluating task.Result will block until the query has completed.

    The simplest option is probably to make the Search method perform the _dispatcher.BeginInvoke call at the end instead.

    Another option – which will become easier with C# 5 – would be to add a continuation to the task, so that when it’s completed you can update the UI. At the moment you’d use Task.ContinueWith; with C# 5 you’d use async and await.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: This is an entire rewrite of the previous question. I have a model.
I have this code which depends on this javascript from dynamic drive: http://dynamicdrive.com/dynamicindex16/formdependency.htm Its
I have assembly A that depends (statically) on type T (reference type, a class)
I'm developing an application in Silverlight2 and trying to follow the Model-View-ViewModel pattern. I
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
NOTE: The libraries MCrypt support depend on have not been updated in years and
Note: Already asked in this post (asked on StackOverflow) . However, the problem is
note from N00B land again. I have read lots about sorting arrays - wanted
Note: I inherited this site, I did not build it. The home page is
NOTE: This question is tricky (to decide whether it pertains to SO or not),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.