Many of the tutorials I've read on async/await feel like they complicate the concept way too much. Ideally, it would allow some time consuming task to be run in the background while the calling method continues its business.
I decided to create the simplest example of async/await I can think of. C# console application:
class Program
{
static void Main(string[] args)
{
PrintTwoTimes();
Console.WriteLine("Main Method!");
Console.ReadLine();
}
async static Task PrintTwoTimes()
{
await Task.Run(() =>
{
Thread.Sleep(500);
Console.WriteLine("Inside Async Task!");
});
Console.WriteLine("After Async Task!");
}
}
The output is as I expected.
The calling thread continues its execution without waiting for PrintTwoTimes() to complete.
What I'd like to know is what is dangerous about such a simple approach to async/await, as I have done, in the real world?
* EDIT *
After being told my question is idiotic, I am removing the bit about static and adding updated code that removes it
class Program
{
static void Main(string[] args)
{
//call the async thread
var prtclss = new PrintClass();
prtclss.PrintTwoTimes();
Console.WriteLine("Main Method!");
Console.ReadLine();
}
}
class PrintClass
{
public string str1 { get; set; }
public string str2 { get; set; }
public PrintClass()
{
str1 = "Inside Async Task!";
str2 = "After Async Task!";
}
public async Task PrintTwoTimes()
{
await Task.Run(() =>
{
//simulate time consuming task
Thread.Sleep(500);
Console.WriteLine(str1);
});
Console.WriteLine(str2);
}
}