0
0

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 1 year has passed since last update.

C#のawaitがよくわからないのでいろいろ試してみた

Posted at
        static async Task Main(string[] args)
        {
            var sw = new Stopwatch();
            TimeSpan ts;

            sw.Start();

            var result1 = await WaitProcess(5).ConfigureAwait(false);
            var result2 = await WaitProcess(3).ConfigureAwait(false);
            var result3 = await WaitProcess(1).ConfigureAwait(false);

            var sum = result1 + result2 + result3;

            sw.Stop();
            ts = sw.Elapsed;

            Console.WriteLine($"sum process took {ts.TotalMilliseconds} milli sec.");

            sw.Restart();

            var result4 = WaitProcess(5);
            var result5 = WaitProcess(3);
            var result6 = WaitProcess(1);

            var res4Result = await result4.ConfigureAwait(false);
            var res5Result = await result5.ConfigureAwait(false);
            var res6Result = await result6.ConfigureAwait(false);

            sum = res4Result + res5Result + res6Result;

            sw.Stop();
            ts = sw.Elapsed;

            Console.WriteLine($"sum process took {ts.TotalMilliseconds} milli sec.");

            sw.Restart();

            var result7 = WaitProcess(5);

            await WaitProcess(7);

            var result8 = WaitProcess(3);
            var result9 = WaitProcess(1);

            var res7Result = await result7;
            var res8Result = await result8;
            var res9Result = await result9;

            sum = res7Result + res8Result + res9Result;

            sw.Stop();
            ts = sw.Elapsed;

            Console.WriteLine($"sum process took {ts.TotalMilliseconds} milli sec.");

            sw.Restart();

            var result10 = WaitProcess(5);
            var result11 = WaitProcess(3);
            var result12 = WaitProcess(1);

            var res10Result = await result10;
            var res11Result = await result11;

            await WaitProcess(7);

            var res12Result = await result12;

            sum = res10Result + res11Result + res12Result;

            sw.Stop();
            ts = sw.Elapsed;

            Console.WriteLine($"sum={sum} sum process took {ts.TotalMilliseconds} milli sec.");
        }
5 sec task Start!
5 sec task Complete!
3 sec task Start!
3 sec task Complete!
1 sec task Start!
1 sec task Complete!
sum process took 9485.2903 milli sec.
5 sec task Start!
3 sec task Start!
1 sec task Start!
1 sec task Complete!
3 sec task Complete!
5 sec task Complete!
sum process took 5013.1635 milli sec.
5 sec task Start!
7 sec task Start!
5 sec task Complete!
7 sec task Complete!
3 sec task Start!
1 sec task Start!
1 sec task Complete!
3 sec task Complete!
sum process took 10035.8225 milli sec.
5 sec task Start!
3 sec task Start!
1 sec task Start!
1 sec task Complete!
3 sec task Complete!
5 sec task Complete!
7 sec task Start!
7 sec task Complete!
sum=9 sum process took 12004.8536 milli sec.

awaitいれると同期的処理になるっていうけど、この実験でだいたい理解できた気がする。。。
というか、やってみると大したことないというか、こういうことかぁって感じ。。。。

以上

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?