locked
How to return result from a cancelled Task

    Question

  • Hi

    Are there any updates or other solutions to this?

    http://social.msdn.microsoft.com/Forums/en-US/e0c3b405-1f9d-4d56-a75a-cb6ed7bc1e9d/returning-result-from-canceled-task?forum=async

    I have the same problems..


    lg

    Monday, February 24, 2014 12:32 PM

All replies

  • The basic idea is still the same. Can you please explain the scenario you are trying to achieve?

    --Rob

    Monday, February 24, 2014 3:56 PM
    Owner
  • I have a operation that can be running up to 5 minutes. It is running as a Task.

    From UI it is possible to cancel the operation by a "Cancel" button.

    I am trying to save the user experience in the case the application us suspended. Basically I am working in this line.

    - If the application is suspended and a there is longrunning Task going.

    - I will cancel the Task.

    - When the Task are going to be cancelled I would like to collect critical parameters and save them.

    - When the application are starting up from suspension,,  I will detect the suspension,, re-load parameters and restart the Task.

    //regards

    //lg


    lg

    Tuesday, February 25, 2014 2:24 PM
  • Could you not just do something like this, in the task:

    if(bCancelled)
    {
         SaveTaskState();
         return;
    }

    Instead of having any caller responsible for collecting and saving the tasks state, have the task function itself save it's state if the bCancelled flag is set due to a suspension or user cancellation.


    Darin R.

    Tuesday, February 25, 2014 4:38 PM
  • this helps a little?

    private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    
            public void OnApplicationStart()
            {
                // check if there was a running operation and restart it..
            }
    
            public void CancelRunningTask()
            {
                cancellationTokenSource.Cancel();
            }
    
            public async void StartOperation()
            {
                var token = cancellationTokenSource.Token;
                try
                {
                    await Task.Factory.StartNew(async () =>
                    {
                        try
                        {
                            while (true)
                            {
                                await Task.Delay(500);
                                token.ThrowIfCancellationRequested();
    
                            }
                        }
                        catch
                        {
                            // Save the task critical values and that the start was running
                            throw;
                        }
                    });
    
                }
                catch { }
            }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, February 25, 2014 4:48 PM
  • Hi

    By "Save the critical values....." I suppose you mean save to disk?

    I already have a simular Task implementation as you propose above. The way I send critical values back to the UI is to use the Progress report.

    So,, I can restart the task from last progress report,, 

    Not a directly clean solution. Is there some obvious advantages/disadvatages using the "disk write"  or "progress report" methods ?

    //regards


    lg

    Wednesday, February 26, 2014 3:23 PM