I'm writing a WPF application (new technique, mostly I've been writing in WinForms). My goal is to make UI responsive whole time, and I've read that it can be achived using Threading/BackgroundWorker. I think that I should use background worker to put there time consuming methods. But I plan to use method *m_AddLog(string logText)* which should append text to textbox. This method I want to call from main UI thread aswell as from background worker, so messages would be sent immediatelly while processing in backround instead of waiting for background task to end. Could you please kindly advise how to write properly write these methods for UI being fully responsive as I don't know much about delegates, invoking, background workers, etc?
-
2Tons of similar questions already here. Essentially you get the UI thread Dispatcher and call Invoke on it to marshal your method across to the UI thread. Its not that complex. – Sep 28 '11 at 12:04
-
http://stackoverflow.com/questions/2676605/reading-text-from-multiple-wpf-textbox-controls-from-a-different-thread – sll Sep 28 '11 at 12:33
-
@Will Maybe I'll clarify a bit more. Main window has two buttons which fires new windows from project for different tasks. One is for unpacking files, etc. I've managed to create a background process using BackgroundWorker and Dispatcher.BeginInvoke, but still UI is not responsive while firing background thread. Also thread does not update the textbox. Here's the code:http://pastebin.com/2njdXCkP – Darj Sep 29 '11 at 08:30
-
@AdrianK.: Sorry, StackOverflow is a question/answer site; we don't debug your code for you. In situations like this, the *absolute best thing you can do* is create a prototype which does exactly what you are trying to do in the minimum amount of code. Not only will this solve your problem half the time, if it doesn't you will be able to succinctly illustrate the issue you are having with this code and will be able to construct a very good question that will be much more likely to attract good answers. I do it *all the time*; I have over 350 such prototypes sitting in my test folder. – Sep 29 '11 at 13:14
3 Answers
If you want to run some background process then update the UI on completion the following pattern works well (if ran from the UI thread).
Task.Factory.StartNew(() =>
{
// Background work
}).ContinueWith((t) => {
// Update UI thread
}, TaskScheduler.FromCurrentSynchronizationContext());
Put the background work in the first task and the UI work in the following his is task. The TaskScheduler option ensures the second task runs on the UI thread.

- 23,264
- 14
- 56
- 75
-
No, no. I'd like to update UI **while** unpacking files with Ionic.Zip framework. – Darj Sep 29 '11 at 07:27
Most of the items in wpf application using task and dispatcher will give better results.
have a Look at the following code hope this may helps you.. In the below code i have considered a scenario like fetching images from remote server and i created a task for doing that... in the task in side for loop i am using dispatched thread to update UI to notify the progress... and after fetching all the images execution will be moved to continue block.... You can have a look at the following link that may helps you to understand it better
ObservableCollection items= new ObservableCollection();
TaskFactory tFactory = new TaskFactory();
tFactory.StartNew(() =>
{
for (int i = 0; i < 50; i++)
{
//Request to server
System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)delegate()
{
// UPDATE PROGRESS BAR IN UI
});
items.Add(("");
}
}).ContinueWith(t =>
{
if (t.IsFaulted)
{
// EXCEPTION IF THREAD IS FAULT
throw t.Exception;
}
System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)delegate()
{
//PROCESS DATA AND DISPLAY
});
});

- 3,436
- 18
- 25
As People Said there are tons of question that will show you how to do that But if you want to compare it you can find it here with detailed comparison

- 1
- 1

- 5,272
- 4
- 30
- 55