0

私は、以下のコード スニペットの concurrency::task の構文を理解しようとしています。

このコード スニペットの構文を理解できません。これをどのように分析しますか:

ここで「getFileOperation」とは何ですか。タイプ StorageFile クラスのオブジェクトですか? ここで「then」キーワードとはどういう意味ですか? その後に「{」がある(....)? この構文を解析できませんか?

また、なぜこの concurrency::task().then().. ユース ケースが必要なのですか?

concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.png"));
   getFileOperation.then([](Windows::Storage::StorageFile^ file)
   {
      if (file != nullptr)
      {

MSDN concurrency::task APIから取得

void MainPage::DefaultLaunch()
{
   auto installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;

   concurrency::task<Windows::Storage::StorageFile^> getFileOperation(installFolder->GetFileAsync("images\\test.png"));
   getFileOperation.then([](Windows::Storage::StorageFile^ file)
   {
      if (file != nullptr)
      {
         // Set the option to show the picker
         auto launchOptions = ref new Windows::System::LauncherOptions();
         launchOptions->DisplayApplicationPicker = true;

         // Launch the retrieved file
         concurrency::task<bool> launchFileOperation(Windows::System::Launcher::LaunchFileAsync(file, launchOptions));
         launchFileOperation.then([](bool success)
         {
            if (success)
            {
               // File launched
            }
            else
            {
               // File launch failed
            }
         });
      }
      else
      {
         // Could not find file
      }
   });
}
4

1 に答える 1

1

getFileOperationStorageFile^将来のある時点で (またはエラー)を返すオブジェクトです。task<t>から返された WinRTIAsyncOperation<T>オブジェクトの C++ラッパーGetFileAsyncです。

の実装はGetFileAsync別のスレッドで実行できますが (必須ではありません)、呼び出し元のスレッドが他の作業 (UI のアニメーション化やユーザー入力への応答など) を続行できるようにします。

このthenメソッドを使用すると、非同期操作が完了すると呼び出される継続関数を渡すことができます。この場合、ラムダ (インライン無名関数) を渡します。これは、[]角かっこで識別され、ラムダ パラメーター リスト (aStorageFile^によって返されるオブジェクトGetFileAsync) と関数本体が続きます。この関数本体はGetFileAsync、将来いつか操作が完了すると実行されます。

に渡される継続関数内のコードは、then通常 (常にではありません)への呼び出しに続くコード(または、この場合はコンストラクター)の後に実行されます。create_task()task

于 2015-04-01T14:50:13.897 に答える