4

我有返回对象的异步方法

public static IEnumerable<Users.User> GetUsers(IEnumerable<string> uids, Field fields)
{
    Task<ResponseApi<Users.User>>[] tasks;
    tasks = uids.Select(uid =>
    {
        var parameters = new NameValueCollection
        {
        {"uids", uid},
        {"fields", FieldsUtils.ConvertFieldsToString(fields)}
        };
        return GetUserResponseApi(parameters);
    }).ToArray();
    Task.WaitAll(tasks);
    foreach(Task<ResponseApi<Users.User>> task in tasks)
    {
        if(task.Result.Response != null)
        {
            yield return task.Result.Response;
        }
    }
}

我想用其他方法更新 UI,UI 可能不会更新,因为 GetUserResponseApi 方法不返回任何值。

 public static Task<ResponseApi<Users.User>> GetUserResponseApi(NameValueCollection parameters)
    {
      return CallMethodApi("users.get", parameters, CallType.HTTPS)
            .ContinueWith(
            r =>
            {
//don't execute
                var responseApi = new ResponseApi<Users.User>();
                responseApi.Response = JsonConvert.DeserializeObject<Users.User>(r.Result["response"][0].ToString());
                return responseApi;

            });
    }

    private void BtnGetUsersClick(object sender, EventArgs e)
    {
        var random = new Random();
        int max = 175028595;
        var uids = new List<string>();
        for(int i = 1; i <= 20; i++)
        {
            uids.Add((random.Next(max) + 1).ToString());

        }
        Task.Factory.StartNew(() =>
                                  {
                                      var users = VkontakteApi.GetUsers(uids, Field.Online);
                                      foreach(var user in users)
                                      {
                                          richTextBox1.Text += string.Format("ID:{0} online:{1}", user.uid,
                                                                             user.online);
                                      }
                                  }, CancellationToken.None, TaskCreationOptions.None, _uiContext);
    }

如何解决 ContinueWith in 的问题GetUserResponseApi

更新: 我认为方法中的问题是GetUserResponseApi因为块 ContinueWith 不执行。

4

2 回答 2

3

Application.Current.Dispatcher每当您访问 UI 对象时,用于调度对 UI 线程的调用。

Application.Current.Dispatcher.Invoke(() => {
try
{
richTextBox1.Text += string.Format("ID:{0} online:{1}", user.uid, user.online);
}
catch
{
   //handle
}
), DispatcherPriority.Background);
于 2012-06-22T12:23:11.727 回答
2

Try using TaskScheduler.FromCurrentSynchronizationContext() method:

Task.Factory.StartNew(() =>
  {
      var users = VkontakteApi.GetUsers(uids, Field.Online);
      foreach(var user in users)
      {
          richTextBox1.Text += string.Format("ID:{0} online:{1}", user.uid,
                                             user.online);
      }
  }, CancellationToken.None,
     TaskScheduler.FromCurrentSynchronizationContext());
于 2012-06-22T12:25:56.667 回答