9
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# で非同期な遅延処理

Last updated at Posted at 2016-04-07

C# でゲームを作っていて、ちょっとした遅延処理を書きたくなったので。

Task.Run(() => {
    Thread.Sleep(200);
    Console.WriteLine("遅延させたい処理。");
});

5.0 以降には async/await という機能もありますが、ドキュメントを読んだ感じ、あちらはもう少し複雑な処理向けという気がしました。

(16/04/08 追記) @kiichi54321 さんよりコメントをいただきました。asyncawait 、わりとカジュアルな使い方もできるんですね。

Task.Run(async () => {
    await Task.Delay(200);
    Console.WriteLine("遅延させたい処理。");
});

async がメソッドなりラムダ式を「非同期に実行する」というマーカーの役割をしていて、その中の実際に非同期にしたい処理に await を付けると、以降の処理が await の結果を待ってくれる感じみたいです。
ここ の第二回の記事とかもとても参考になりました。

ご指摘ありがとうございました。

9
17
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?