我的任务是执行一些繁重的工作。
我需要将结果传递给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的数据,因为我会对这样的代码有异常。
所以,我的问题是如何让它正常工作?
答案 0 :(得分:16)
这是一篇好文章:Parallel Programming: Task Schedulers and Synchronization Context。
示例:
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.
}
答案 1 :(得分:5)
您需要在UI线程上运行ContinueWith -task。这可以使用具有overloaded version of the ContinueWith -method的UI线程的TaskScheduler来完成,即
TaskScheduler scheduler = TaskScheduler.Current;
...ContinueWith(obj => LogContent = obj.Result), CancellationToken.None, TaskContinuationOptions.None, scheduler)
答案 2 :(得分:1)
您可以使用Dispatcher
在UI线程上调用代码。看一下文章Working With The WPF Dispatcher
答案 3 :(得分: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;
}