Concurrency Windows通用C++;并发性

Concurrency Windows通用C++;并发性,concurrency,win-universal-app,c++-cx,Concurrency,Win Universal App,C++ Cx,我有以下模式: task<Param1^> MyClass::MethodA(String^ s) { return create_task([this, s] { .... IAsyncOperation<Param1^>^ result = SystemMethod1ReturningParam1(...); return result; }); } IAsyncOperati

我有以下模式:

task<Param1^> MyClass::MethodA(String^ s)
{
     return create_task([this, s]
     {

         ....

         IAsyncOperation<Param1^>^ result = SystemMethod1ReturningParam1(...);

         return result;
     });
}

IAsyncOperation<Param2^>^ MyClass::MethodB(String^ s)
{
    return create_async([this, s]
    {
         return MethodA(s).then([this](task<Param1^> t)
         {
             Param1^ p1 = t.get();

             ....

             Param2^ result = SystemMethod2ReturningParam2(p1);
             return result ;
         });
    });
}
任务MyClass::MethodA(字符串^s)
{
返回创建任务([this,s]
{
....
IAsyncOperation^result=SystemMethod1ReturningParam1(…);
返回结果;
});
}
IAsyncOperation ^MyClass::MethodB(字符串^s)
{
返回create_async([this,s]
{
返回方法A。然后([此](任务t)
{
Param1^p1=t.get();
....
Param2^result=SystemMethod2ReturningParam2(p1);
返回结果;
});
});
}
我称之为public MethodB,它称之为private MethodA。Param1是我无法更改的标准枚举。方法创建任务我需要返回错误条件。我不能返回null。MethodB还需要返回错误条件

从两种方法返回错误的最佳方法是什么


SystemMethod1ReturningParam1和SystemMethod2ReturningParam2是框架系统方法,它们可以引发异常。我在哪里捉到它们

如果您担心异常,应该始终使用基于任务的延续,并将对
task.get()
的调用包装在
try/catch

注意下面的代码(和其他C++代码)抛出 STD::异常< /code >,但是如果您与WRET组件接口,并且希望异常传播,则必须重新抛出为平台::异常< /code >值。

concurrency::task<int> throw_async()
{
  return concurrency::create_task([]
  {
    throw std::exception("oops");
    return -1;
  });
}

void test_value()
{
  // This will crash inside PPL runtime due to unobserved exception
  throw_async().then([](int x)
  {
    OutputDebugString(L"You'll never see this\r\n");
  });
}

void test_task()
{
  // This will handle the exception correctly
  throw_async().then([](concurrency::task<int>& t)
  {
    OutputDebugString(L"You'll see this\r\n");
    try
    {
      auto val = t.get();
      OutputDebugString(L"You'll never see this\r\n");
    }
    catch (std::exception& ex)
    {
      auto error = ex.what(); // "oops"
      // Need to re-throw if this crosses the WinRT ABI boundary.
      // Conversion of 'error' to Platform::String^ omitted for brevity
      throw ref new Platform::FailureException(/* error */);
    }
  });
}
concurrency::task throw\u async()
{
返回并发::创建_任务([]
{
抛出std::异常(“oops”);
返回-1;
});
}
无效测试_值()
{
//由于未观察到的异常,这将在PPL运行时内崩溃
抛出异步()
{
OutputDebugString(L“您永远不会看到这个\r\n”);
});
}
无效测试任务()
{
//这将正确处理异常
抛出异步()
{
OutputDebugString(L“您将看到此\r\n”);
尝试
{
auto val=t.get();
OutputDebugString(L“您永远不会看到这个\r\n”);
}
捕获(标准::异常和例外)
{
自动错误=例如what();/“oops”
//如果它跨越WinRT ABI边界,则需要重新抛出。
//为简洁起见,省略了将“error”转换为Platform::String^
throw ref new Platform::FailureException(/*error*/);
}
});
}

我不喜欢这样说,但是学习WC++是C++的一种错误做法。试着用标准来理解它。我精通经典C++和经典并发。然后,请问一个重点问题,显示你试图使用那里的东西。当然,代码不是C++。它看起来像一个叫做C++/CLI的东西,它是一个完全不同的野兽。它似乎不是这样工作的。如果我抛出std::exception,当我尝试访问task.get时,我将捕获Pltform::exception,并以E_FAIL和“Unspecified error”作为描述。