How to keep streaming continuously - Tweetinvi

2019-05-16 21:53发布

I adapted the following code from one I found online. Works ok but is not running continuously and pulling in new tweets. What do I need to change? Help appreciated.

private static void Stream_FilteredStreamExample()
{
    SqlConnection conn = new SqlConnection(@"Data Source=********************");
    conn.Open();

    for (; ; )
    {
        try
        {
            var stream = Stream.CreateFilteredStream();
            //stream.AddLocation(Geo.GenerateLocation(-180, -90, 180, 90));
            stream.AddTweetLanguageFilter(Language.English);
            stream.AddTrack("#TubeStrike");

            var timer = Stopwatch.StartNew();

            stream.MatchingTweetReceived += (sender, args) =>
            {

                var tweet = args.Tweet;

                if (timer.ElapsedMilliseconds > 1000)
                {
                    Console.WriteLine("{0}, {1}", tweet.IdStr, tweet.Text);
                    timer.Restart();
                }
            };

            stream.StartStreamMatchingAllConditions();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex.Message);
        }
    }
}

1条回答
一夜七次
2楼-- · 2019-05-16 22:41

You want to use the following code:

stream.StreamStopped += (sender, args) =>
{
     stream.StartStreamMatchingAllConditions();
};
查看更多
登录 后发表回答