locked
Cancelling an async task without a CancellationToken

    Question

  • I have seen a number of examples of how to write asynchronous tasks which include the CancellationToken to allow them to be cancelled. My problem is finding a simple way to cancel a method such as:

                    MapLocationFinderResult result =
                        await MapLocationFinder.FindLocationsAsync(
                                            addressToGeocode,
                                            hintPoint,
                                            resultCount);

    This call is made inside an async method which is called from an event handler.

    I want to present a busy indicator and a CANCEL button on the screen but I need a way to cancel the "await" when the button is clicked (and of course to know it has been cancelled so I can prevent some other actions from occurring).

    If I work my way far enough through the inheritances then I eventually come to a Cancel method in the IAsyncInfo interface but I don't know if or how this can be used.

    Any help would be appreciated.


    Woke up one day and realised reality television had destroyed too many brain cells so started writing App's to re-start my brain.

    Sunday, September 28, 2014 9:27 AM

Answers

  • Try this

    public class Sample
    {
        private System.Threading.CancellationTokenSource findLocationCancellationSource;
        private Task<bool> findAndProcessLocationsTask;
    
        public bool CancelFindLocation()
        {
            bool result = false;
    
            if (findLocationCancellationSource != null
                && findAndProcessLocationsTask != null
                && !findAndProcessLocationsTask.IsCompleted
                && !findLocationCancellationSource.IsCancellationRequested)
            {
                findLocationCancellationSource.Cancel();
    
                result = true;
            }
    
            return result;
        }
    
        public async Task<bool> HandleFindLocation(String addressToGeocode, Geopoint hintPoint, UInt32 resultCount)
        {
            findLocationCancellationSource = new System.Threading.CancellationTokenSource();
    
            findAndProcessLocationsTask = Task.Run(async () => await findAndProcessLocationsAsync(addressToGeocode, hintPoint,
                resultCount, findLocationCancellationSource.Token), findLocationCancellationSource.Token);
    
            await findAndProcessLocationsTask;
    
            return true;
        }
    
        private async Task<bool> findAndProcessLocationsAsync(String addressToGeocode, Geopoint hintPoint, UInt32 resultCount,
            System.Threading.CancellationToken cancellationToken)
        {
            MapLocationFinderResult result = null;
    
            if (!cancellationToken.IsCancellationRequested)
            {
                result = await MapLocationFinder.FindLocationsAsync(
                                            addressToGeocode,
                                            hintPoint,
                                            resultCount);
            }
    
            if (result != null && !cancellationToken.IsCancellationRequested)
            {
                await prosessing1(result);
            }
    
            if (result != null && !cancellationToken.IsCancellationRequested)
            {
                await prosessing2(result);
            }
    
            if (result != null && !cancellationToken.IsCancellationRequested)
            {
                await prosessing3(result);
            }
    
            return true;
        }
    
        private async Task<bool> prosessing1(MapLocationFinderResult result)
        {
            await Task.Delay(1);
    
            return true;
        }
    
        private async Task<bool> prosessing2(MapLocationFinderResult result)
        {
            await Task.Delay(1);
    
            return true;
        }
    
        private async Task<bool> prosessing3(MapLocationFinderResult result)
        {
            await Task.Delay(1);
    
            return true;
        }
    }


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    Monday, September 29, 2014 6:17 AM