locked
Cancel an async request

    Question

  • Hello Everybody !

    I have this code below : 

    var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
                        var period = TimeSpan.FromSeconds(2);
                        ThreadPoolTimer PeriodicTimer = null;
                        PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async (source) =>
                        {
    
                               
                                await dispatcher.RunAsync(CoreDispatcherPriority.High,
                                   async () =>
                                   {
    
                                      
                                       EtatCreationDataBaseResponse responseEtatCreationDataBase = await _middlewareService.GetEtatCreationDatabase(FileName);
    
                                      //TODO WHEN GetEtatCreationDatabase SERVICE RETURN VALUES
                                   });
                          
    
                        },
                          period);

    In this case, the GetEtatCreationDatabase WebService executes every 2 seconds, the problem is that if the WebService doesn't answer in 2 seconds, I have many calls of this WebService with no answers.

    I need to delete all currents (with no answers yet) GetEtatCreationDatabase WebServices when I have one response from the first call of GetEtatCreationDatabase WebService.

    Somebody may help please?

    Please do not send a link but an answer with code.

    Thanx very much !!

    Wednesday, January 7, 2015 9:47 PM

Answers

  • Hello Diddy,

    Sorry for the delay. Please take a look at the comments in the code snippets.

    //Each webservice call would require a new CancellationTokenSource and CancellationToken
        var tokenSource = new CancellationTokenSource();
        CancellationToken token = tokenSource.Token;
       
        //Implement some timer logic to check for long running processes
        //Start the timer logic when GetEtatCreationDatabase method is called
    
        public async Task<EtatCreationDataBaseResponse> GetEtatCreationDatabase(String idrSite, CancellationToken cancelToken)
        {
            //Check the task status if it has been cancelled already
            if (cancelToken.IsCancellationRequested)
            {
                // Clean up here, then...
                cancelToken.ThrowIfCancellationRequested();
            }
    
            EtatCreationDataBaseResponse response = null;
    
            List<string> urlParameters = new List<string>();
            urlParameters.Add(idrSite);
    
            response = await ExecuteRequest<EtatCreationDataBaseResponse>(Middleware.SiteEtatRequest, true, HttpMethod.Get, urlParameters);
    
            return response;
        }
    
        public void SomeTimerLogic(CancellationTokenSource cancelsource)
        {
            //If timer is exceeded cancel the task
            cancelsource.Cancel();
        }
    Hope this helps.


    Abdulwahab Suleiman


    Wednesday, January 14, 2015 12:28 AM
    Moderator

All replies

  • Hello Diddy,

    This can be accomplished by converting your "GetEtatCreationDatabase" method to a task and pass in a cancellationtoken. This token would be used to track the status of the task.

    You'd need to create an instance of the "CancellationTokenSource" and get the token from this object (e.g tokenSource). The task can be cancelled by calling tokenSource.Cancel(). Then in your method, you can check for "IsCancellationRequested == true" and handle appropriately. One way you can handle this is to return a status "success" or "no answer yet" (according to your requirements). The other way is to throw a "ThrowIfCancellationRequested()" and handle that way.

    Here's a good reference on how to Cancel a Task and Its Children.

    Let me know if this helps.


    Abdulwahab Suleiman

    Thursday, January 8, 2015 2:05 AM
    Moderator
  • Hello Abdulwahab,

    Could you tell me how to do it please?

    Here is the code of GetEtatCreationDatabase : 

            public async Task<EtatCreationDataBaseResponse> GetEtatCreationDatabase(String idrSite)
            {
                EtatCreationDataBaseResponse response = null;
    
                List<string> urlParameters = new List<string>();
                urlParameters.Add(idrSite);
    
                response = await ExecuteRequest<EtatCreationDataBaseResponse>(Middleware.SiteEtatRequest, true, HttpMethod.Get, urlParameters);
    
                return response;
            }

    Thanx very much

    Thursday, January 8, 2015 8:23 AM
  • Hello Diddy,

    Sorry for the delay. Please take a look at the comments in the code snippets.

    //Each webservice call would require a new CancellationTokenSource and CancellationToken
        var tokenSource = new CancellationTokenSource();
        CancellationToken token = tokenSource.Token;
       
        //Implement some timer logic to check for long running processes
        //Start the timer logic when GetEtatCreationDatabase method is called
    
        public async Task<EtatCreationDataBaseResponse> GetEtatCreationDatabase(String idrSite, CancellationToken cancelToken)
        {
            //Check the task status if it has been cancelled already
            if (cancelToken.IsCancellationRequested)
            {
                // Clean up here, then...
                cancelToken.ThrowIfCancellationRequested();
            }
    
            EtatCreationDataBaseResponse response = null;
    
            List<string> urlParameters = new List<string>();
            urlParameters.Add(idrSite);
    
            response = await ExecuteRequest<EtatCreationDataBaseResponse>(Middleware.SiteEtatRequest, true, HttpMethod.Get, urlParameters);
    
            return response;
        }
    
        public void SomeTimerLogic(CancellationTokenSource cancelsource)
        {
            //If timer is exceeded cancel the task
            cancelsource.Cancel();
        }
    Hope this helps.


    Abdulwahab Suleiman


    Wednesday, January 14, 2015 12:28 AM
    Moderator