locked
cancellation of a Continous Task RRS feed

  • Question

  • Hi:

            I had this Exception whenever I cancel a continuous task.

    "Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll" 

    then  follow by "OperationCanceledException thrown with message: A task was canceled."  

    Is that acceptable ?

    here are my test code:

    private async void T(bool start,CancellationTokenSource tokensource)
            {
                var token = tokensource.Token;
                var task= Task.Run(async () =>
                {
                    while (start)
                    {
                       
                            token.ThrowIfCancellationRequested();
                            DoWork();
                            if (token.IsCancellationRequested)
                            {
                                start = false;
                                token.ThrowIfCancellationRequested();
                                tokensource.Cancel();
                            }
                            Console.WriteLine("c {0}", hvalue[0].ToString());
                            await Task.Delay(100, tokensource.Token);
                       
                        
                           
                       
                      
                    }
                
                },tokenSource.Token);
    
                try
                {
                    await task;
                }
                catch (OperationCanceledException ex)
                {
                    start = false;
                    Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {ex.Message}");
                }
                finally
                {
                    tokenSource.Dispose();
                }
            }
            private void DoWork()
            {
                
                    counter += 1;
                   
               
               
            }

    How can i handle this exceptioon (Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll) or just ignore it?

    Thanks

    Simon

    Monday, October 19, 2020 7:02 AM

Answers

  • OperationCanceledException is the base class of TaskCanceledException.
    You are throwing it in your code:
    token.ThrowIfCancellationRequested();
    but then also handling it:
    catch (OperationCanceledException ex)

    So I don't quite understand your question.
    • Marked as answer by JetSimon Tuesday, October 20, 2020 1:21 AM
    Monday, October 19, 2020 2:43 PM

All replies

  • OperationCanceledException is the base class of TaskCanceledException.
    You are throwing it in your code:
    token.ThrowIfCancellationRequested();
    but then also handling it:
    catch (OperationCanceledException ex)

    So I don't quite understand your question.
    • Marked as answer by JetSimon Tuesday, October 20, 2020 1:21 AM
    Monday, October 19, 2020 2:43 PM
  • Hello,

    Check out the following code sample done in a Windows Form project. The key here is to use a try/catch so what is presented can be done in any project type with changes e.g. here I do it in a button click event, in other projects this might be implemented differently in ASP.NET etc..

    The following code checks for a cancel request while the following code starts the task and handles the cancellation.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 19, 2020 2:52 PM
  • H Karen:

         I am having a continuous loop, which started when the window loaded.

    The cancellation worked but I saw an exception at the output window, "Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll ". i am quite sure it is not capture in the try catch

    So I am curious where is exception come from.

    Monday, October 19, 2020 3:23 PM
  • H Karen:

         I am having a continuous loop, which started when the window loaded.

    The cancellation worked but I saw an exception at the output window, "Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll ". i am quite sure it is not capture in the try catch

    So I am curious where is exception come from.

    Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.dll is coming from a .NET DLL which stands for Microsoft Common Object Runtime Library.

    What you see is a debugger thing, see 

    https://github.com/grpc/grpc/issues/11328

    https://stackoverflow.com/questions/1973149/a-first-chance-exception-of-type-system-threading-threadabortexception-occurre


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, October 19, 2020 4:15 PM
  • LoL:

            I probably think too much.... That what the ThrowifCancellationRequested all about !

    Tuesday, October 20, 2020 1:21 AM