locked
Can cancel or stop a Task? RRS feed

  • Question

  • User384128 posted

    if I use a Task to run somethings ,can I cancel it some way? maybe like:

    int main()
    {
    CreatBox();
    }
    
    async void CreatBox()
    {
         for(int i=0:i<100;i++)
        {
        Task.Delay(1000);
        StackLayout.Children.Add(new BoxView());
        }
    }
    
        private void _Button_A_Clicked(object sender, EventArgs e)
        {
            //cancel <CreatBox>,no matter loop times
        }
    
    Monday, June 3, 2019 10:07 AM

Answers

  • User200920 posted

    Use CancellationTokenSource. https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, June 3, 2019 10:12 AM
  • User89714 posted

    @ClawRage

    You use a CancellationToken. For an example of how to use a Task with a CancellationToken, see the code below, which is from (or based on) the Xamarin Sport sample.

    ``` public async Task RunSafe( Task task, bool notifyOnError = true, [CallerMemberName] string caller = "") { Exception exception = null;

            try
            {
                await Task.Run(async () =>
                {
                    if (!CancellationToken.IsCancellationRequested)
                    {
                        task.Start();
                        task.Wait(CancellationToken);
                    }
                }, CancellationToken);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("Task Canceled");
            }
            catch (AggregateException e)
            {
                AggregateException ex = e.Flatten();
                exception = ex.InnerException;
            }
            catch (Exception e)
            {
                exception = e;
            }
    
            if (exception != null)
            {
                if (notifyOnError)
                {
                    NotifyException(exception);
                }
            }
        }
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, June 3, 2019 10:14 AM

All replies

  • User200920 posted

    Use CancellationTokenSource. https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, June 3, 2019 10:12 AM
  • User89714 posted

    @ClawRage

    You use a CancellationToken. For an example of how to use a Task with a CancellationToken, see the code below, which is from (or based on) the Xamarin Sport sample.

    ``` public async Task RunSafe( Task task, bool notifyOnError = true, [CallerMemberName] string caller = "") { Exception exception = null;

            try
            {
                await Task.Run(async () =>
                {
                    if (!CancellationToken.IsCancellationRequested)
                    {
                        task.Start();
                        task.Wait(CancellationToken);
                    }
                }, CancellationToken);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("Task Canceled");
            }
            catch (AggregateException e)
            {
                AggregateException ex = e.Flatten();
                exception = ex.InnerException;
            }
            catch (Exception e)
            {
                exception = e;
            }
    
            if (exception != null)
            {
                if (notifyOnError)
                {
                    NotifyException(exception);
                }
            }
        }
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, June 3, 2019 10:14 AM
  • User379860 posted

    @ClawRage Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

    Tuesday, June 4, 2019 7:04 AM