I
I
Ignatiy22020-10-05 18:43:13
C++ / C#
Ignatiy2, 2020-10-05 18:43:13

Confusion with async functions?

Initially, I had the impression that the main difference between asynchrony and multithreading is that asynchrony does not use additional threads. That is, I thought that it was created for I / O operations, where operations are not done by the processor, but, for example, by the server. But I went to metanit and saw this thing ( https://metanit.com/sharp/tutorial/13.3.php ):

static void Factorial()
        {
            int result = 1;
            for(int i = 1; i <= 6; i++)
            {
                result *= i;
            }
            Thread.Sleep(8000);
            Console.WriteLine($"Факториал равен {result}");
        }
        // определение асинхронного метода
        static async void FactorialAsync()
        {
            Console.WriteLine("Начало метода FactorialAsync"); // выполняется синхронно
            await Task.Run(()=>Factorial());                // выполняется асинхронно
            Console.WriteLine("Конец метода FactorialAsync");
        }
 
        static void Main(string[] args)
        {
            FactorialAsync();   // вызов асинхронного метода
 
            Console.WriteLine("Введите число: ");
            int n = Int32.Parse(Console.ReadLine());
            Console.WriteLine($"Квадрат числа равен {n * n}");
             
            Console.Read();
        }

And my whole picture crumbled, confusion appeared. After all, in order to calculate the factorial, some kind of computing power is needed, it turns out that some kind of flow is allocated here? It after all already will be a multithreading, in my understanding. Explain, please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Pavlov, 2020-10-05
@Ignatiy2

Read more about asynchrony here. Its mission is to reduce delays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question