1

I have already searched for lots of questions and answers, but still not manage to fix the issue yet.

CancellationTokenSource not working for me, cuz any cancellationtokensource method ends the async request within the async method. Not from the outside.

Let me expand what I want to tell you;

I have a Hammock REST method whick asyncronously streams Twitter for any tweets like below:

IAsyncResult asyncServ = service.StreamFilter((streamEvent, response) =>
            {
                if (streamEvent == null && response != null && response.StatusCode != 0)
                {
                    string logDesc = String.Format("Failed to Stream {0}:{1}", obj.Keyword, response.Response);
                    methods.CreateLog(ClassLibrary.LogType.Error, ClassLibrary.LogSource.TweetKeywordStreamService, logDesc, null);
                }
                else if (response == null || streamEvent == null)
                {
                    methods.CreateLog(ClassLibrary.LogType.Error, ClassLibrary.LogSource.TweetKeywordStreamService, "Failed to Stream: " + obj.Keyword, null);
                }
                else if (response.StatusCode == 0)
                {
                    Task objSaver = new Task(() =>
                    {
                        SaveStreamTweet(streamEvent, obj.Id, obj.Keyword);
                    });

                    objSaver.Start();
                }
                else
                {
                    methods.CreateLog(ClassLibrary.LogType.Error, ClassLibrary.LogSource.TweetKeywordStreamService, "Possibly failed to Stream: " + obj.Keyword, null);
                }

            }, obj.Keyword, obj.Follow);

This method only triggered only if a tweet received. So If I use cancellation token inside Stream Filter method, I need to wait for a tweet to be triggered and just then my code block works.

I want to end this service outside the Stream Filter code block.

Here is how StreamFilter method works, it creates and returns an async beginrequest with IAsyncResult type.(Actually this one triggered only if a tweet received)

return client.BeginRequest(request, new RestCallback<T>((req, resp, state) =>
        {
            Exception exception;
            var entity = TryAsyncResponse(() => 
                    {

                        SetResponse(resp);

                        JsonSerializer serializer = new JsonSerializer();
                        var retEntity = serializer.Deserialize<T>(resp);
                        return retEntity;
                     },
                    out exception);
            action(entity, new TwitterResponse(resp, exception));
        }));

I have tried aborting Thread, but no ways, this beginrequest is handled in another thread that I'm not inside of it.

Couldn't use EndRequest method, cuz it throws an error.

Thanks in advance

ilerleartik
  • 115
  • 8

0 Answers0