\( \newcommand{\N}{\mathbb{N}} \newcommand{\R}{\mathbb{R}} \newcommand{\C}{\mathbb{C}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\P}{\mathcal P} \newcommand{\B}{\mathcal B} \newcommand{\F}{\mathbb{F}} \newcommand{\E}{\mathcal E} \newcommand{\brac}[1]{\left(#1\right)} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\matrixx}[1]{\begin{bmatrix}#1\end {bmatrix}} \newcommand{\vmatrixx}[1]{\begin{vmatrix} #1\end{vmatrix}} \newcommand{\lims}{\mathop{\overline{\lim}}} \newcommand{\limi}{\mathop{\underline{\lim}}} \newcommand{\limn}{\lim_{n\to\infty}} \newcommand{\limsn}{\lims_{n\to\infty}} \newcommand{\limin}{\limi_{n\to\infty}} \newcommand{\nul}{\mathop{\mathrm{Nul}}} \newcommand{\col}{\mathop{\mathrm{Col}}} \newcommand{\rank}{\mathop{\mathrm{Rank}}} \newcommand{\dis}{\displaystyle} \newcommand{\spann}{\mathop{\mathrm{span}}} \newcommand{\range}{\mathop{\mathrm{range}}} \newcommand{\inner}[1]{\langle #1 \rangle} \newcommand{\innerr}[1]{\left\langle #1 \right \rangle} \newcommand{\ol}[1]{\overline{#1}} \newcommand{\toto}{\rightrightarrows} \newcommand{\upto}{\nearrow} \newcommand{\downto}{\searrow} \newcommand{\qed}{\quad \blacksquare} \newcommand{\tr}{\mathop{\mathrm{tr}}} \newcommand{\bm}{\boldsymbol} \newcommand{\cupp}{\bigcup} \newcommand{\capp}{\bigcap} \newcommand{\sqcupp}{\bigsqcup} \newcommand{\re}{\mathop{\mathrm{Re}}} \newcommand{\im}{\mathop{\mathrm{Im}}} \newcommand{\comma}{\text{,}} \newcommand{\foot}{\text{。}} \)

Sunday, December 22, 2019

JavaScript's new Promise and Promise.all counterpart in C#

The result starts with:
and then simultaneously:

task 2 waits for task 1, and then run after 4 seconds, so the event fired at the 6nd second.
taks 3 starts at the beginning, and fired at the 6nd second.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace TestAsync
{
    class Program
    {
        static void Main(string[] args)
        {
            var task1 = Task.Factory.StartNew(
                () =>
                {
                    Thread.Sleep(2000);
                    Console.WriteLine("Task 1 done!");
                    return 1;
                });
            var task2 = Task.Factory.StartNew(
             async () =>
                {
                    await task1;
                    Thread.Sleep(4000);
                    Console.WriteLine("Task 2 done!");
                    return 2;
                });
            var task3 = Task.Factory.StartNew(
                () =>
                {
                    Thread.Sleep(6000);
                    Console.WriteLine("Task 3 done!");
                    return 3;
                });

            var aTimer = new System.Timers.Timer();
            aTimer.Interval = 100;
            aTimer.Elapsed += (source, args) => { Console.Write("."); };
            aTimer.AutoReset = true;
            aTimer.Enabled = true;

            Task.WhenAll(task1, task2, task3).ContinueWith(results =>
            {
                aTimer.Enabled = false;
                int result = task1.Result + task2.Result.Result + task3.Result;
                Console.WriteLine($"result: {result}");
            });

            Console.ReadLine();

        }
    }
}

No comments:

Post a Comment