Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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# ContinueWith不尊重奥尔良单线程特性_C#_Asp.net_Actor_Orleans - Fatal编程技术网

C# ContinueWith不尊重奥尔良单线程特性

C# ContinueWith不尊重奥尔良单线程特性,c#,asp.net,actor,orleans,C#,Asp.net,Actor,Orleans,我有以下代码: 有人能解释一下吗?当存在ContinueWith时是否违反了单线程性质?未违反单线程性质。项目中的编译警告明确了问题的来源。特别是:此异步方法缺少“await”运算符,将同步运行。考虑使用“Acess”操作符等待非阻塞API调用,或“等待任务…运行…”在后台线程上执行CPU绑定的工作。< /P> 方法async Task Call_A_ToTemp从不等待对grain B的调用。相反,它在发出调用后立即返回。由于Call_A_ToTemp返回的任务会立即完成,因此允许对该粒度执行

我有以下代码:


有人能解释一下吗?当存在ContinueWith时是否违反了单线程性质?

未违反单线程性质。项目中的编译警告明确了问题的来源。特别是:此异步方法缺少“await”运算符,将同步运行。考虑使用“Acess”操作符等待非阻塞API调用,或“等待任务…运行…”在后台线程上执行CPU绑定的工作。< /P> 方法async Task Call_A_ToTemp从不等待对grain B的调用。相反,它在发出调用后立即返回。由于Call_A_ToTemp返回的任务会立即完成,因此允许对该粒度执行另一个调用。一旦grain.CallA完成,将继续使用。。。将尽快在谷物的TaskScheduler上执行,例如,当谷物正在等待另一个调用或处于空闲状态时

相反,如果等待调用,或者从方法中删除了async,并且代码更改为返回grain.CallA.ContinueWith。。。调用,然后将观察到预期的行为。也就是说,将代码更改为此将获得预期结果:

// removed 'async' here, since we're not awaiting anything.
// using 'async' is preferred, but this is to demonstrate a point about
// using ContinueWith and un-awaited calls
public Task Call_A_ToTemp()
{
   Console.WriteLine("Making call A to a fellow grain");
   IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);

   // Note the 'return' here
   return grain.CallA().ContinueWith((t)=>
   {
     if(t.IsFaulted)
     {
       // Silo message timeout is 32s so t.IsFaulted is true
       Console.WriteLine("Task Faulted");
       Call_A_ToTemp();
     }
    });
}

没有违反单线程特性。项目中的编译警告明确了问题的来源。特别是:此异步方法缺少“await”运算符,将同步运行。考虑使用“Acess”操作符等待非阻塞API调用,或“等待任务…运行…”在后台线程上执行CPU绑定的工作。< /P> 方法async Task Call_A_ToTemp从不等待对grain B的调用。相反,它在发出调用后立即返回。由于Call_A_ToTemp返回的任务会立即完成,因此允许对该粒度执行另一个调用。一旦grain.CallA完成,将继续使用。。。将尽快在谷物的TaskScheduler上执行,例如,当谷物正在等待另一个调用或处于空闲状态时

相反,如果等待调用,或者从方法中删除了async,并且代码更改为返回grain.CallA.ContinueWith。。。调用,然后将观察到预期的行为。也就是说,将代码更改为此将获得预期结果:

// removed 'async' here, since we're not awaiting anything.
// using 'async' is preferred, but this is to demonstrate a point about
// using ContinueWith and un-awaited calls
public Task Call_A_ToTemp()
{
   Console.WriteLine("Making call A to a fellow grain");
   IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);

   // Note the 'return' here
   return grain.CallA().ContinueWith((t)=>
   {
     if(t.IsFaulted)
     {
       // Silo message timeout is 32s so t.IsFaulted is true
       Console.WriteLine("Task Faulted");
       Call_A_ToTemp();
     }
    });
}

继续,继续。。。将尽快在grain的TaskScheduler上执行“尽快”意味着什么?当前任务在调度程序上结束后?准确地说。每个激活都有一个自定义TaskScheduler,负责逐个执行任务。所以当继续与。。。已准备好运行,其继续任务将在调度程序上排队并执行。我正在做更多的实验,还有另一个相关问题。你能看一下这个吗?继续。。。将尽快在grain的TaskScheduler上执行“尽快”意味着什么?当前任务在调度程序上结束后?准确地说。每个激活都有一个自定义TaskScheduler,负责逐个执行任务。所以当继续与。。。已准备好运行,其继续任务将在调度程序上排队并执行。我正在做更多的实验,还有另一个相关问题。你能看看这个吗?
public async Task Call_A_ToTemp()
{
    Console.WriteLine("Making call A to a fellow grain");
    IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);

    bool isSuccess = false;
    while (! isSuccess)
    {
       try
       {
          await grain.CallA();
          isSuccess = true;
       } catch(TimeoutException){
            Console.WriteLine("task faulted");
       }

    }
}
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Task Faulted                       
Making call A to a fellow grain
// removed 'async' here, since we're not awaiting anything.
// using 'async' is preferred, but this is to demonstrate a point about
// using ContinueWith and un-awaited calls
public Task Call_A_ToTemp()
{
   Console.WriteLine("Making call A to a fellow grain");
   IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);

   // Note the 'return' here
   return grain.CallA().ContinueWith((t)=>
   {
     if(t.IsFaulted)
     {
       // Silo message timeout is 32s so t.IsFaulted is true
       Console.WriteLine("Task Faulted");
       Call_A_ToTemp();
     }
    });
}