0

I trying to create a Winforms app in VS for search a specific keyword and retrieve the tweet display in my app. Below is my code which trying to get 100 tweet from my profile and make a call to the new method in my form's constructor, followed by assignment of the data to the main tweets list box on my form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using LinqToTwitter; 

    public partial class Form1 : Form
        {
            private SingleUserAuthorizer authorizer =
             new SingleUserAuthorizer
             {
                 CredentialStore = new
                SingleUserInMemoryCredentialStore
                 {
                     ConsumerKey ="",
                     ConsumerSecret ="",
                     AccessToken ="",
                     AccessTokenSecret =""
                 }
             };

            private List<Status> currentTweets;
            public Form1()
            {
                InitializeComponent();

                GetMostRecent100HomeTimeLine();
                lstTweetList.Items.Clear();
                currentTweets.ForEach(tweet =>
                   lstTweetList.Items.Add(tweet.Text));
            }
            private void GetMostRecent100HomeTimeLine()
            {
                var twitterContext = new TwitterContext(authorizer);

                var tweets = from tweet in twitterContext.Status
                             where tweet.Type == StatusType.Home &&
                             tweet.Count == 100
                             select tweet;

                currentTweets = tweets.ToList();
            }

[An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll] are occur inside the code below

currentTweets = tweets.ToList();

My guess is it's the LinqToTwitter library trying to enumerate the 'tweets' collection and finding there's either no data, or the data cannot be retrieved. I am beginner to this topic and this is my project.

Hope someone can guide me to solve the problem

1 Answers1

0

LINQ to Twitter is async. So you should use async and await keywords in your code. It also means it isn't a good idea to put the query in the constructor. In this case, I would use the Form.Load event. Assuming, you hooked up a Form1_Load handler to the Form1 Load event, you could re-write the code like this:

        public async void Form1_Load()
        {
            await GetMostRecent100HomeTimeLine();
            lstTweetList.Items.Clear();
            currentTweets.ForEach(tweet =>
               lstTweetList.Items.Add(tweet.Text));
        }
        private async Task GetMostRecent100HomeTimeLine()
        {
            var twitterContext = new TwitterContext(authorizer);

            var tweets = from tweet in twitterContext.Status
                         where tweet.Type == StatusType.Home &&
                         tweet.Count == 100
                         select tweet;

            currentTweets = await tweets.ToListAsync();
        }

Notice that the Form1_Load method is async. It also returns void, which is necessary and should only be used in top level async event handlers. It awaits the task returned by GetMostRecent100HomeTimeLine.

The GetMostRecent100HomeTimeLine method is async too, but returns Task. All of your methods in the async call chain, below the initial async void event handler, should return Task or Task<T>. This lets you await their tasks so that the calling method doesn't return before the called method is complete.

Finally, notice that the code awaits the task returned from ToListAsync. While the compiler lets you use ToList (without the await), it won't work at runtime because LINQ to Twitter is async and expects you to await it's operators, like ToListAsync, SingleOrDefaultAsync, or FirstOrDefaultAsync.

You can also use try/catch handlers to catch any exceptions thrown. LINQ to Twitter wraps most exceptions in a TwitterQueryException that will contain any error messages and associated codes from the Twitter API.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60