【问题标题】:TPL Problems, Error-Handling and Return ValuesTPL 问题、错误处理和返回值
【发布时间】:2010-12-22 20:57:49
【问题描述】:

我尝试了几个小时来实现这样的场景,但使用输入参数和返回值。

这很好,我得到了我的期望:

public class AsyncStuff2
{
    public void DoAsyncStuff()
    {            
        TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

        Task myTask = Task.Factory.StartNew(() =>
        {
            OperationXy();
        });

        bool hadError = false;

        myTask = myTask.ContinueWith(errorTest =>
        {
            Console.WriteLine("Faulted");
            hadError = true;

            if (errorTest.Exception != null)
            {
                Console.WriteLine(errorTest.Exception.Message);
            }
        }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, uiScheduler);

       myTask.ContinueWith(another =>
        {
            Console.WriteLine("Done");

            if (hadError)
            {
                Console.WriteLine("...but with error");
            }

        }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, uiScheduler);

    }

    private void OperationXy()
    {
        Console.WriteLine("OperationXY");
        throw new ArgumentException("Just for Test");
    }

输出将是这样的:

操作XY 故障 发生一个或多个错误。 完毕 ...但有错误

但是当我修改这个例子时,任务继续不能像我一样工作,除了:

public class AsyncStuff
{

    public string Path { get; set; }

    public void DoAsyncStuff()
    {
        Path = "A Input...";

        TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

        Task<string> myTask = Task<string>.Factory.StartNew((input) =>
        {
            return OperationXy(Path);

        }, Path, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);

        bool hadError = false;

        myTask = myTask.ContinueWith<string>(errorTest =>
        {
            Console.WriteLine("Faulted");
            hadError = true;

            if (errorTest.Exception != null)
            {
                Console.WriteLine(errorTest.Exception.Message);
            }

            return null;

        }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, uiScheduler);

        myTask.ContinueWith(another =>
        {
            Console.WriteLine("Done, Result: {0}", myTask.Result);

            if (hadError)
            {
                Console.WriteLine("...but with error");
            }

        }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, uiScheduler);
    }

    private string OperationXy(string returnThat)
    {
        Console.WriteLine("OperationXY, Input ({0})", returnThat);

        //throw new ArgumentException("Just for Test");

        return returnThat;
    }

}

我想要实现的是:

  • 将处理所需的输入传递给任务
  • 将结果设置为 UI 元素
  • 处理错误,但仍继续“OnlyOnRanToCompletion”

任何帮助表示赞赏

谢谢

马丁

【问题讨论】:

    标签: c# task parallel-processing task-parallel-library


    【解决方案1】:

    这是因为您的代码中存在错误。您在创建错误处理延续时重新定义了 myTask。行:

            myTask = myTask.ContinueWith(errorTest =>
    

    应阅读:

            myTask.ContinueWith(errorTest =>
    

    否则,您会将运行完成延续添加到错误处理延续,而不是原始 myTask。

    这应该可以修复您的代码。输出现在应该是:

    OperationXY 
    Done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-02
      • 2016-06-25
      • 2013-01-23
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 2014-07-31
      • 2019-05-11
      相关资源
      最近更新 更多