2012-10-29 46 views

回答

11

任務的想法是,你可以把它們連:

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 
    } 
1

你需要給調度呼叫轉移到任務的延續,它會是這個樣子:

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

它看起來像你正在開始一個後臺任務,開始閱讀推文,然後開始另一項任務來閱讀結果,而不需要兩者之間的協調。

我希望你的任務是在延續(見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));