Hi all. There is such a code. Tell me how to make it less cumbersome? Maybe add some refactoring?
Thread.Sleep is needed so that I can have time to enter the data necessary to run the methods.
static void Main()
{
Console.Write("Enter the path for data invaiting: ");
BackgroundTest shortTest = new BackgroundTest(Console.ReadLine());
var task1 = Task.Run(() => shortTest.RunLoop());
Thread.Sleep(100000);
Console.Write("Enter the path for data invaiting: ");
BackgroundTest longTest = new BackgroundTest(Console.ReadLine());
var task2 = Task.Run(() => longTest.RunLoop());
Thread.Sleep(100000);
Console.Write("Enter the path for data invaiting: ");
BackgroundTest veryLongTest = new BackgroundTest(Console.ReadLine());
var task3 = Task.Run(() => veryLongTest.RunLoop());
task1.Wait();
task2.Wait();
task3.Wait();
}
Rajanikant HawaldarPosted Aug 24, 2022, 8:33 AM
Hello Manoj,
1) Console.ReadLine pauses execution until a line is entered, there's no need to add Thread.Sleep, and if what you want is to wait until you have entered all the data before each task is run, then it is not doing that either anyway
2) static async Task Main() should be the signature of the method, you need it to be async for the next point
3. Task.Wait() is not how you do asynchronous work, you should be doing await task1;, await task2; and await task3; instead
You could also take out the three calls to and add them into a method, such as:
And then, in main
or better