WPF: how to call a method in a separate thread with a delay time

I have a TextBox that a user can enter a search term into it. Its binding to string Term property in my view-model . And I want to do a search query when its contents have changed. But I want to make a request in a separate thread with a delay .

eg. when the user enters a letter, I want to wait 0.3 seconds , and if the user changes the input during this time (0.3 seconds), the timer resets and starts again. Otherwise, I start a new thread and execute a search query. During query execution, if the user changes the term again, interrupt the previous query and run it again.

I know how to do this in windows forms with streams and Timer . But I'm new to WPF , and I am looking to see if there is a way defined for streaming WPF functions (or maybe there is a way with better performance).

Do you have any ideas? Can you help me?

+4
source share
3 answers

You can use DispatcherTimer . At each key press, stop the timer, if it is already running, then start it. I believe (and you have to check it out!) That will reset it.

If it fires, then you take the current value in the text box and start the operation in a separate thread (for example, using Task.Factory.StartNew if you use .NET 4 or BackgroundWorker ), or just create a new thread).

Basically, it separates the “new thread” part from the “I really want to do something” part, keeping everything in the user interface thread until the moment you decide that you really want to do something (and know the value that you want to use )

+2
source

You might want to check out Reactive Extensions from Microsoft. Rx provides a way to combine these types of events into a single event as soon as some delay has passed.

Phil Haack (formerly Microsoft) had a good blog article where he talks about the possibility of throttling.

0
source

It is simply based on what John Skets said. Give him a check. .stop() displays a reset timer .

 public MainWindow() { InitializeComponent(); backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = true; backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 4); } public string Input { get { return input; } set { if (value == input) return; value = value.Trim(); input = value; NotifyPropertyChanged("Input"); if (backgroundWorker1.IsBusy) backgroundWorker1.CancelAsync(); dispatcherTimer.Stop(); dispatcherTimer.Start(); } } private void dispatcherTimer_Tick(object sender, EventArgs e) { dispatcherTimer.Stop(); if (!backgroundWorker1.IsBusy) { backgroundWorker1.RunWorkerAsync(Input); } } 
0
source

Source: https://habr.com/ru/post/1411636/


All Articles