locked
Canceled async status

Answers

  • It's not so much that you have to "cancel the task too" as it is that you need to complete the task in a way that's clear to create_async that it received and responded to the client's request to cancel. The call to cancel the operation is a request for cancellation. The asynchronous operation itself (your create_async lambda) continues to run until it completes or responds to the cancellation via a special exception which cancel_current_task (among other things) generates. From create_async's perspective, the fact that the original code continued to run and successfully produced a result (the value 42) seems to indicate that the operation completed successfully despite the request to cancel. Since there is a result produced, the operation is marked as successfully completed.

    The additional code that you've added is one very explicit way to respond to the request to cancel. If you are interested, the MSDN documentation around create_async has significantly more details on this (as well as relevant links): http://msdn.microsoft.com/en-us/library/hh750082(v=vs.110).aspx

    -- Bill



    Friday, April 13, 2012 5:56 PM

All replies

  • Hello,

    I tested your codes and I found that you did not cancel that task, after you call the  WinRTComponent::CancelOperation(). It will return as normal and it is still the completed status.

    You should call cancel_current_task(); to cancel that task.

    Windows::Foundation::IAsyncOperation<int>^ WinRTComponent::DoLongOperation()
    {
    	
    	return create_async([this](cancellation_token ct) -> int {
    
    		while(!ct.is_canceled());
    		cancel_current_task();
    		return 42;
    	});
    }
    

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, April 13, 2012 7:38 AM
  • I see … I find it a little unintuitive that I have to cancel the task too. I would expect that to happen automatically.

    This is what I have ended up with (keep in mind that this is just a sample, the while loop represents some work that happens in plain C++ code, and I don't want to pollute that with the token):

    Windows::Foundation::IAsyncOperation<int>^ WinRTComponent::DoLongOperation()
    {
    	return create_async([this](cancellation_token ct) -> int {
    		ct.register_callback([this](){
    			CancelOperation();
    		});
    		
    		while(!isStopped){}
    
    		if(ct.is_canceled()){
    			cancel_current_task();
    		}
    		return 42;
    	});
    }


    Valentin Iliescu

    Friday, April 13, 2012 2:44 PM
  • It's not so much that you have to "cancel the task too" as it is that you need to complete the task in a way that's clear to create_async that it received and responded to the client's request to cancel. The call to cancel the operation is a request for cancellation. The asynchronous operation itself (your create_async lambda) continues to run until it completes or responds to the cancellation via a special exception which cancel_current_task (among other things) generates. From create_async's perspective, the fact that the original code continued to run and successfully produced a result (the value 42) seems to indicate that the operation completed successfully despite the request to cancel. Since there is a result produced, the operation is marked as successfully completed.

    The additional code that you've added is one very explicit way to respond to the request to cancel. If you are interested, the MSDN documentation around create_async has significantly more details on this (as well as relevant links): http://msdn.microsoft.com/en-us/library/hh750082(v=vs.110).aspx

    -- Bill



    Friday, April 13, 2012 5:56 PM