Answer the question
In order to leave comments, you need to log in
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();
}
Answer the question
In order to leave comments, you need to log in
Read more about asynchrony here. Its mission is to reduce delays.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question