About closing User Streams

Sep 4, 2011 at 5:48 AM

Hi.

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

Thanks.

Coordinator
Sep 4, 2011 at 9:04 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Sep 4, 2011 at 9: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)
                {
                    strm.CloseStream();
                }
            })
            .SingleOrDefault();

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

                Thread.Sleep(10000);
            }

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

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.

@JoeMayo 

 

Sep 5, 2011 at 7: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.

azyobuzin

Coordinator
Sep 6, 2011 at 2:46 PM

Hi,

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

Joe

Coordinator
Sep 6, 2011 at 6:09 PM

azyobuzin,

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?

Joe

Sep 7, 2011 at 7:55 AM

Yes, the problem disappeared.

Thank you very much!

azyobuzin