This project has moved and is read-only. For the latest updates, please go here.

About closing User Streams

Sep 4, 2011 at 6:48 AM


The trouble is that User Streams takes time to close.
How to close User Streams immediately, when LinqToTwitter is blocking to wait next response?


Sep 4, 2011 at 10:04 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 4, 2011 at 10:18 PM

Hi azyobuzin,

I see what you're referring to.  The request was blocking, which prevented closing the stream.  Twitter sends empty responses on occasion, but those aren't predictable.  I've modified steaming to give you better control over when to close the stream and checked the code into ChangeSet 92316.

The new implementation allows you to set TwitterContext.ReadWriteTimeout to the number of milliseconds that the stream will block, waiting on a response.  After this timeout period, the code will check to see if you want to close the stream and behave accordingly.  If you don't want to close the stream, LINQ to Twitter will continue waiting for more data.  Here's an example:

        private static void UserStreamWithTimeoutDemo(TwitterContext twitterCtx)
            twitterCtx.ReadWriteTimeout = 3000;
            StreamContent strmCont = null;

            Console.WriteLine("\nStreamed Content: \n");
            int count = 0;

            // the user stream is for whoever is authenticated
            // via the Authenticator passed to TwitterContext
            (from strm in twitterCtx.UserStream
             where strm.Type == UserStreamType.User
             select strm)
            .StreamingCallback(strm =>
                strmCont = strm;
                Console.WriteLine(strm.Content + "\n");

                if (count++ >= 25)

            while (strmCont == null)
                Console.WriteLine("Waiting on stream to initialize.");


            Console.WriteLine("Stream is initialized. Now closing...");

The code above shows how to close the stream from both within the callback and outside.  Since the query returns to it's caller immediately, I added a while loop to wait until StreamContent is instantiated.



Sep 5, 2011 at 8:32 AM

I think that it differs from my expectation...

The trouble is that The thread of User Streams remains, so the process cannot be ended at once.
And, because the user closes User Streams, I cannot get timing.

I really appreciate all your trouble.


Sep 6, 2011 at 3:46 PM


Could you provide the steps that I can take to see the same issue you're seeing?


Sep 6, 2011 at 7:09 PM


Looking closer at this, I realized I was running the stream on a foreground thread.  I changed this and did another check-in, changeset 92378, so that streaming threads run as background threads, on the thread pool.

Does this help?


Sep 7, 2011 at 8:55 AM

Yes, the problem disappeared.

Thank you very much!