Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TPL问题、错误处理和返回值_C#_Task_Parallel Processing_Task Parallel Library - Fatal编程技术网

C# TPL问题、错误处理和返回值

C# TPL问题、错误处理和返回值,c#,task,parallel-processing,task-parallel-library,C#,Task,Parallel Processing,Task Parallel Library,我花了几个小时来实现这样一个场景,但是有输入参数和返回值 这很好,我得到了我所期望的: public class AsyncStuff2 { public void DoAsyncStuff() { TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Task myTask = Task.Factory.StartNe

我花了几个小时来实现这样一个场景,但是有输入参数和返回值

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

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;
    }
公共类异步
{
公共字符串路径{get;set;}
公共物品
{
Path=“输入…”;
TaskScheduler uiScheduler=TaskScheduler.FromCurrentSynchronizationContext();
Task myTask=Task.Factory.StartNew((输入)=>
{
返回操作xy(路径);
},路径,CancellationToken.None,TaskCreationOptions.None,TaskScheduler.Default);
bool-haderro=false;
myTask=myTask.ContinueWith(错误测试=>
{
控制台。写入线(“故障”);
haderro=true;
if(errorTest.Exception!=null)
{
Console.WriteLine(errorTest.Exception.Message);
}
返回null;
},CancellationToken.None,TaskContinuationOptions.OnlyOnFaulted,uiScheduler);
myTask.ContinueWith(另一个=>
{
WriteLine(“完成,结果:{0}”,myTask.Result);
if(hadrerror)
{
Console.WriteLine(“…但有错误”);
}
},CancellationToken.None,TaskContinuationOptions.OnlyOnRanToCompletion,uiScheduler);
}
私有字符串操作XY(字符串返回)
{
WriteLine(“OperationXY,输入({0})”,returnThat;
//抛出新ArgumentException(“仅用于测试”);
归还;
}
}

我想要实现的是:

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

谢谢


Martin

这是因为您的代码中有一个bug。在创建错误处理延续时,可以重新定义myTask。该行:

        myTask = myTask.ContinueWith(errorTest =>
应改为:

        myTask.ContinueWith(errorTest =>
否则,您将把“运行到完成”继续添加到错误处理继续,而不是原始myTask

这将修复您的代码。输出现在应为:

OperationXY 
Done