首页 > 解决方案 > Output.analysis = 等待任务;在视频帧分析示例中返回异常

问题描述

我正在测试新的Microsoft 认知服务示例,代码如下:

FrameGrabber当我尝试时,它会在第 518 行返回一个异常:

output.Analysis = await task; 

分析为空,但是,它在第​​ 78 行声明为:

public AnalysisResultType Analysis { get; set; } = default(AnalysisResultType);

运行示例的任何建议?

/// <summary> Executes the analysis operation asynchronously, then returns either the
      ///     result, or any exception that was thrown. </summary>
      /// <param name="frame"> The frame. </param>
      /// <returns> A Task<NewResultEventArgs> </returns>
      protected async Task<NewResultEventArgs> DoAnalyzeFrame(VideoFrame frame)
      {
          CancellationTokenSource source = new CancellationTokenSource();

          // Make a local reference to the function, just in case someone sets
          // AnalysisFunction = null before we can call it.
          var fcn = AnalysisFunction;
          if (fcn != null)
          {
              NewResultEventArgs output = new NewResultEventArgs(frame);
              var task = fcn(frame);
              LogMessage("DoAnalysis: started task {0}", task.Id);
              try
              {
                  if (task == await Task.WhenAny(task, Task.Delay(AnalysisTimeout, source.Token)))
                  {
                      output.Analysis = await task;
                      source.Cancel();
                  }
                  else
                  {
                      output.TimedOut = true;
                  }
              }
              catch (Exception ae)
              {
                  output.Exception = ae;
              }

              LogMessage("DoAnalysis: returned from task {0}", task.Id);

              return output;
          }
          else
          {
              return null;
          }
      }

它应该返回分析结果,但它返回异常

标签: c#apiasync-awaitartificial-intelligencemicrosoft-cognitive

解决方案


推荐阅读