SoFunction
Updated on 2025-03-07

C# uses Task to implement parallel programming

Story background

The sunlight outside the gauze window was shining, and it was another Monday.

Take your time

When I saw the time, it was still early, so I jumped around

  • dressing
  • brush teeth
  • Wash your face

In terms of code, it should be like this:

// 
using System;
using ;
using ;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ("Three things to get up early to start...");
            Stopwatch sw = new Stopwatch();
            ();
            Dress();
            BrushTeeth();
            WashFace();
            ();
            ($"...Get up early and get done three things, Total time consumption {} Second");
            ();
        }

        /// <summary>
        /// Dress up        /// </summary>
        static void Dress()
        {
            ($"Dressing starts...");
            Stopwatch sw = new Stopwatch();
            ();
            (timeout: (value: 1));
            ();
            ($"...Dressing is done, time consuming {} Second");
        }

        /// <summary>
        /// brush teeth        /// </summary>
        static void BrushTeeth()
        {
            ($"Brushing your teeth starts...");
            Stopwatch sw = new Stopwatch();
            ();
            (timeout: (value: 3));
            ();
            ($"...Finished brushing, time consuming {} Second");
        }

        /// <summary>
        /// Wash your face        /// </summary>
        static void WashFace()
        {
            ($"Share your face to start...");
            Stopwatch sw = new Stopwatch();
            ();
            (timeout: (value: 5));
            ();
            ($"...Series Completed, time consuming {} Second");
        }
    }
}

After running, wait for a while and you will see the following output:

Get up early and start with three things...
Dressing begins...
...It takes 1 second to finish the dressing
Brushing your teeth begins...
...It takes 3 seconds to brush your teeth
Wash your face to start...
...Series completed, taking 5 seconds
...It takes 9 seconds to get up early

Take your time one by one, it takes 9 seconds...

Hurry up

When I saw the time, Oh my go, I'm almost late, dress and brush your teeth and wash your face together... Don't ask me how it is achieved in reality

In short, the code is like this:

// 
using System;
using ;
using ;
using ;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ("Three things to get up early to start...");
            Stopwatch sw = new Stopwatch();
            ();
            Task dressTask = (action: Dress);
            Task brushTeethTask = (action: BrushTeeth);
            Task washFaceTask = (action: WashFace);
            (dressTask, brushTeethTask, washFaceTask);
            ();
            ($"...Get up early and get done three things, Total time consumption {} Second");
            ();
        }

        /// <summary>
        /// Dress up        /// </summary>
        static void Dress()
        {
            ($"Dressing starts...");
            Stopwatch sw = new Stopwatch();
            ();
            (timeout: (value: 1));
            ();
            ($"...Dressing is done, time consuming {} Second");
        }

        /// <summary>
        /// brush teeth        /// </summary>
        static void BrushTeeth()
        {
            ($"Brushing your teeth starts...");
            Stopwatch sw = new Stopwatch();
            ();
            (timeout: (value: 3));
            ();
            ($"...Finished brushing, time consuming {} Second");
        }

        /// <summary>
        /// Wash your face        /// </summary>
        static void WashFace()
        {
            ($"Share your face to start...");
            Stopwatch sw = new Stopwatch();
            ();
            (timeout: (value: 5));
            ();
            ($"...Series Completed, time consuming {} Second");
        }
    }
}

After starting and running, wait for a while and you should see the following output:

Get up early and start with three things...
Brushing your teeth begins...
Wash your face to start...
Dressing begins...
...It takes 1 second to finish the dressing
...It takes 3 seconds to brush your teeth
...Series completed, taking 5 seconds
...It takes 5 seconds to get up early

As you can see, I did a few things together, and it took only 5 seconds.

Let's summarize

Do a few things together (in parallel), take it slowly than one thing

  • It saves time, but writing more code will take more energy.
  • Can be usedTaskTo easily implement parallel operations.
  • ...

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links