10

I have a task that performing some heavy work. I need to path it's result to LogContent

Task<Tuple<SupportedComunicationFormats, List<Tuple<TimeSpan, string>>>>.Factory
    .StartNew(() => DoWork(dlg.FileName))
    .ContinueWith(obj => LogContent = obj.Result);

This is the property:

public Tuple<SupportedComunicationFormats, List<Tuple<TimeSpan, string>>> LogContent
{
    get { return _logContent; }
    private set
    {
        _logContent = value;
        if (_logContent != null)
        {
            string entry = string.Format("Recognized {0} log file",_logContent.Item1);
            _traceEntryQueue.AddEntry(Origin.Internal, entry);
        }
    }
}

Problem is that _traceEntryQueue is data bound to UI, and of cause I will have exception on code like this.

So, my question is how to make it work correctly?

Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

17

Here is a good article: Parallel Programming: Task Schedulers and Synchronization Context.

Take a look at Task.ContinueWith() method.

Example:

var context = TaskScheduler.FromCurrentSynchronizationContext();
var task = new Task<TResult>(() =>
    {
        TResult r = ...;
        return r;
    });

task.ContinueWith(t =>
    {
        // Update UI (and UI-related data) here: success status.
        // t.Result contains the result.
    },
    CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, context);

task.ContinueWith(t =>
    {
        AggregateException aggregateException = t.Exception;
        aggregateException.Handle(exception => true);
        // Update UI (and UI-related data) here: failed status.
        // t.Exception contains the occured exception.
    },
    CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, context);

task.Start();

Since .NET 4.5 supports async/await keywords (see also Task.Run vs Task.Factory.StartNew):

try
{
    var result = await Task.Run(() => GetResult());
    // Update UI: success.
    // Use the result.
}
catch (Exception ex)
{
    // Update UI: fail.
    // Use the exception.
}
5

You need to run the ContinueWith -task on the UI thread. This can be accomplished using the TaskScheduler of the UI thread with the overloaded version of the ContinueWith -method, ie.

TaskScheduler scheduler = TaskScheduler.Current;
...ContinueWith(obj => LogContent = obj.Result), CancellationToken.None, TaskContinuationOptions.None, scheduler)
bobblez
  • 1,340
  • 20
  • 35
1

You can use the Dispatcher to invoke code on UI thread. Take a look at the article Working With The WPF Dispatcher

RePierre
  • 9,358
  • 2
  • 20
  • 37
0

If you are using async/await, then here is some example code that shows how to schedule a task to run on the GUI thread. Place this code at the bottom of the stack of all of your async/await calls to avoid the WPF runtime throwing errors with code not executing on the GUI thread.

Works with WPF + MVVM, tested under VS 2013.

public async Task GridLayoutSetFromXmlAsync(string gridLayoutAsXml)
{
    Task task = new Task(() => // Schedule some task here on the GUI thread );
    task.RunSynchronously();
    await task;
}
Contango
  • 76,540
  • 58
  • 260
  • 305
  • Thanks, this was exactly what I was trying to achieve. A simple way to run a method async but still be able to update the main thread. I played around with Await Task.Run() but got nothing. – LievenV Oct 15 '15 at 10:18
  • Word of warning - `Task.Run` is a heavyweight operation, it creates a new thread and gobbles up a lot of stack space. Use sparingly. – Contango Oct 15 '15 at 15:43