如何从任务更新UI线程?

10

我有一个需要执行一些繁重工作的任务。

我需要将其结果传递给LogContent

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

这是属性:

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);
        }
    }
}

问题在于_traceEntryQueue是与 UI 绑定的数据,因此在像这样的代码中我会遇到异常。

那么,我的问题是如何使它正常工作?


你使用哪种UI?WPF、Windows Forms、Silverlight、Web? - Alexander Schmidt
4个回答

17

这里有一篇很好的文章:并行编程:任务调度器和同步上下文.

看一下Task.ContinueWith() 方法.

例如:

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();

自从.NET 4.5支持async/await关键字(也可以参见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

您需要在 UI 线程上运行 ContinueWith 任务。这可以使用 UI 线程的 TaskScheduler 通过 重载版本的 ContinueWith 方法 来完成。

TaskScheduler scheduler = TaskScheduler.Current;
...ContinueWith(obj => LogContent = obj.Result), CancellationToken.None, TaskContinuationOptions.None, scheduler)

1

0
如果您正在使用async/await,那么这里有一些示例代码,展示了如何在GUI线程上安排任务。将此代码放置在所有async/await调用的堆栈底部,以避免WPF运行时抛出代码未在GUI线程上执行的错误。
适用于WPF + MVVM,在VS 2013下测试通过。
public async Task GridLayoutSetFromXmlAsync(string gridLayoutAsXml)
{
    Task task = new Task(() => // Schedule some task here on the GUI thread );
    task.RunSynchronously();
    await task;
}

谢谢,这正是我想要实现的。一种简单的方法可以异步运行一个方法,但仍然能够更新主线程。我尝试了使用 Await Task.Run() 但没有得到任何结果。 - LievenV
警告 - Task.Run 是一个重量级的操作,它会创建一个新线程并占用大量堆栈空间。请谨慎使用。 - Contango

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接