C# 任务结果事件已完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13124329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:40:09  来源:igfitidea点击:

Event on Task Result is done

c#.netwpftask-parallel-library

提问by Developer

Possible Duplicate:
How to create a task (TPL) running a STA thread?

可能的重复:
如何创建运行 STA 线程的任务 (TPL)?

I'm using the following code:

我正在使用以下代码:

var task = Task.Factory.StartNew<List<NewTwitterStatus>>(
        () => GetTweets(securityKeys),  
        TaskCreationOptions.LongRunning);

Dispatcher.BeginInvoke(DispatcherPriority.Background,
    new Action(() =>
    {
        var result = task.Result; // ERROR!!! The calling thread cannot access this object because a different thread owns it.
        RecentTweetList.ItemsSource = result;
        Visibility = result.Any() ? Visibility.Visible : Visibility.Hidden;
    }));

And I'm getting the error:

我收到错误:

var result = task.Result; // ERROR!!! The calling thread cannot access this object because a different thread owns it.

What do I need to do to resolve this problem?

我需要做什么来解决这个问题?

采纳答案by Henk Holterman

The idea of Tasks is that you can chain them:

任务的想法是你可以链接它们:

  var task = Task.Factory.StartNew<List<NewTwitterStatus>>(
                            () => GetTweets(securityKeys),  
                            TaskCreationOptions.LongRunning
                        )
        .ContinueWith(tsk => EndTweets(tsk) );


    void EndTweets(Task<List<string>> tsk)
    {
        var strings = tsk.Result;
        // now you have your result, Dispatchar Invoke it to the Main thread
    }

回答by Trevor Pilley

You need to move the Dispatcher call into the task continuation which would look something like this:

您需要将 Dispatcher 调用移动到任务延续中,它看起来像这样:

var task = Task.Factory
    .StartNew<List<NewTwitterStatus>>(() => GetTweets(securityKeys), TaskCreationOptions.LongRunning)
    .ContinueWith<List<NewTwitterStatus>>(t =>
    {
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            new Action(() =>
            {
                var result = t.Result;
                RecentTweetList.ItemsSource = result;
                Visibility = result.Any() ? Visibility.Visible : Visibility.Hidden;
            }));
    },
    CancellationToken.None,
    TaskContinuationOptions.None);

回答by AlSki

It looks like you are starting a background task to start reading tweets, then starting another task to read the result without any co-ordination between the two.

看起来您正在启动一个后台任务来开始阅读推文,然后开始另一个任务来阅读结果,两者之间没有任何协调。

I would expect your task to have another task in a continuation (see http://msdn.microsoft.com/en-us/library/dd537609.aspx) and in the continuation you may need to invoke back to the UI thread....

我希望您的任务在延续中有另一个任务(请参阅http://msdn.microsoft.com/en-us/library/dd537609.aspx)并且在延续中您可能需要调用回 UI 线程.. ..

var getTask = Task.Factory.StartNew(...);
var analyseTask = Task.Factory.StartNew<...>(
()=> 
Dispatcher.Invoke(RecentTweetList.ItemsSource = getTask.Result));