Accessing Task context of faulted async method?

Locked Accessing Task context of faulted async method?

  • 2012年1月17日 20:22
     
     

    I have an aync method that returns Task<T> and I had set ContinueWith on returned task. When task is completed I can get info from result but when faulted I would like to do some action in context of this task but AsyncSytate is null because this task is created automatically by Async CTP. How can I access to context of my Task when it is faulted?

全部回复

  • 2012年1月18日 4:37
    版主
     
     
    What state is it that you want the continuation to have?  Where does that state come from?  Can you pass it into both the async method and into the continuation?
  • 2012年1月18日 12:36
     
     

    Little unprofessional but I put in Word :)

    http://tfs.exaco.pl/xxx/sample_code.zip

    I highlighted place when I need context of operation

  • 2012年1月19日 14:57
     
     

    Maybe I rewrite this here. I have async function DowloadTestAsync and when it is done with error I should do something - for instance dlete it from _Tasks list. But when exception is thrown I don't know context of the task

        var context = new TaskContext();

                lock (tasksSync)
                {
                    _Tasks.Add(context);
                }

                IsolateStorage.FixSpace();
               
                context.Task = Task.Factory.StartNew((a) =>
                {
                    DowloadTestAsync(a as TaskContext, null).ContinueWith((res) =>
                    {
                        if (res.IsFaulted)
                        {

                            // NO CONTEXT
                            if (ErrorInfo != null)
                            {
                                ErrorInfo(res.Exception);
                            }
                        }
                        else if (res.IsCanceled)
                        {

                        }
                        else if(res.IsCompleted)
                        {
                            QuestionsReady(res.Result);
                        }
                       
                    }
                    );

                }, context, context.CanselationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

     

     private async Task<TestModel> DowloadTestAsync(TaskContext context, int? test_id)
            {
                if (StartDownload != null)
                {
                    StartDownload();
                }

                var testModel = await PrepareQuestionList(context.CanselationToken.Token, test_id);

               
                if (QuestionNumberReady != null)
                {
                    QuestionNumberReady(testModel.NumberOfQuestions);
                }

                var test = await GetFullQuestionListAsync(testModel, test_id.HasValue, ResumeMode.Off, context.CanselationToken.Token);


                context.TestModel = test;

                return test;
            }



    • 已编辑 dominikj 2012年1月19日 14:58
    • 已编辑 dominikj 2012年1月19日 14:59
    •  
  • 2012年1月20日 0:16
    版主
     
     已答复

    Thanks for sharing the example (I couldn't download from the link you previously provided).

    Why can't you access 'a' or 'context' from within your ContinueWith delegate?  In .NET 4 you could access them via a closure (created for you implicitly), and in .NET 4.5 you can pass it into the continuation as object state (as .NET 4.5 includes new ContinueWith overloads that accept object state).

    Or am I misunderstanding?

    • 已标记为答案 dominikj 2012年1月20日 8:34
    •  
  • 2012年1月20日 8:34
     
     

    Hmm You are right…. I think that I miss this because debugger in CTP sometimes works strange ;) But there are some thing’s to add. I watched videos with You about Tasks and closure and I tried to avoid them with passing object state, but Task returned from async method is created automatically and there is no way to pass state to this Task and read it from ContineWith. Beside of this I tried to find ContinueWith overload with state but this is Silverlight project and I think there is no overload like this. Beside of this I can use closure :)


    • 已编辑 dominikj 2012年1月20日 8:35
    •