2014-03-04 155 views
0

我正在使用Linq到支持异步和等待功能的Twitter。如何使用异步和等待关键字异步呼叫

我赢形式的应用程序,它调用DLL里面的方法来获得关注。(目前,我只是回返回一个整数,但我想回到追随者,不知道如何做到这一点的一部分)

private void SubmitButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 

       MessageBox.Show("Please wait for few mins"); 

       var maxFollowers = (MaxFollowers.Text == "") ? (int?)null : int.Parse(MaxFollowers.Text); 

       var followers = TwitterHelper.GetFollowersTweets 
        (Convert.ToUInt64(TwitterUserID.Text), ConsumerKey.Text, ConsumerSecretKey.Text, 
         AccessToken.Text, AccessTokenSecret.Text, maxFollowers); 


       MessageBox.Show(string.Format("Total Followers Found: {0}", followers.ToString())); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(string.Format("Something went wrong!")); 
      } 
     } 

其是从表格呼叫

public static int GetFollowersTweets 
     (ulong twitterUserId, string consumerKey, string consumerKeySecret, 
      string accessToken, string accessTokenSecret, int? maxFollowers) 
     { 


      var auth = GetUserAuthorizationToken 
       (consumerKey, consumerKeySecret, 
        accessToken, accessTokenSecret); 

      var followers = GetTwitterFollowers 
       (twitterUserId, auth, maxFollowers); 

      var followersTweets = new List<TwitterData>(); 

      followers.ContinueWith((taskWithMsg) => 
            { 
             followersTweets = GetFollowersTweetsList(taskWithMsg.Result, auth); 
            }); 



      return 2; 


      // return followersTweets; 

     } 

主要方法,其从Twitter API “GetTwitterFollowers”

private static async Task<List<TwitterData>> GetTwitterFollowers(
     ulong twitterUserId, SingleUserAuthorizer auth, int? maxFollowers) 
     { 

      var followerss = maxFollowers ?? 15000; 

      try 
      { 
       var twitterCtx = new TwitterContext(auth); 
       var followers = await twitterCtx.Friendship 
              .Where(f => f.Type == FriendshipType.FollowersList 
               && f.UserID == twitterUserId.ToString()) 
              .Select(f => new TwitterData() 
              { 

               Followers = f.Users.Where(t => !t.Protected).Take(followerss).Select(s => s).ToList() 
              }).SingleOrDefaultAsync(); 


       return GetFollowersList(followers.Followers); 


      } 
      catch (Exception ex) 
      { 
       return null; 
      } 

     } 
取追随者的方法

目前,我的应用程序的流程是这样的

1)当从我的形式提交按钮点击,它调用的公共方法“GetFollowersTweets”,然后调用内部方法“GetTwitterFollowers”。而这种内部方法是异步,所以当它开始提取追随者时,控制器会再次返回到公共方法,并且它会简单地向表单上的调用者返回一个整数...并且在某些时候,当Twitter取回追随者时,它会从其他任务中恢复下面的行

followers.ContinueWith((taskWithMsg) => 
             { 
              followersTweets = GetFollowersTweetsList(taskWithMsg.Result, auth); 
             }); 

正如你所看到的,我只是返回一个int,而是我想返回“followersTweets”,我该怎么做?需要进行哪些更改?请帮助

回答

1

一般来说,不要使用ContinueWith在异步代码中;使用await代替:

var followers = await GetTwitterFollowersAsync(twitterUserId, auth, maxFollowers); 
var followersTweets = await GetFollowersTweetsListAsync(followers, auth); 
return followersTweets; 

我也改变了你的异步方法在“异步”,by convention结束。

这需要你的方法的签名改变:

public static async Task<List<TwitterData>> GetFollowersTweetsAsync(...) 

以及您的事件处理程序的签名:

private async void SubmitButton_Click(object sender, EventArgs e) 
{ 
    ... 
    var followers = await TwitterHelper.GetFollowersTweetsAsync 
     (Convert.ToUInt64(TwitterUserID.Text), ConsumerKey.Text, ConsumerSecretKey.Text, 
     AccessToken.Text, AccessTokenSecret.Text, maxFollowers); 
    ... 
} 
+0

我只是做了这些改变。但它不会将控制权归还给Form ..代码在程序集内正确执行。任何建议? – user2866746

+0

我把一个Messagebox.show后,var追随者...在形式.. – user2866746

+0

你确定LINQ to Twitter支持'async'吗? –

0

而不是

 var followers = GetTwitterFollowers(twitterUserId, auth, maxFollowers); 

     var followersTweets = new List<TwitterData>(); 

     followers.ContinueWith((taskWithMsg) => 
           { 
            followersTweets = GetFollowersTweetsList(taskWithMsg.Result, auth); 
           }); 



     return 2; 

使用

var followers = await GetTwitterFollowers(twitterUserId, auth, maxFollowers); 
var followersList = await GetFollowersTweetsList(followers, auth); 
return followersList.Count; 

此外,你应该改变GetFollowersTweets()的签名

public static async Task<int> GetFollowersTweets