I wanted to experiment with async and await but I had a fiasco. I have the method which need to run two methods asynchronously(it works partly)
public void Run()
{
Task[] tasks = new Task[2];
tasks[0] = Task.Factory.StartNew(DisplayInt);
tasks[1] = Task.Factory.StartNew(DisplayString);
//block thread until tasks end
Task.WaitAll(tasks);
}
And two methods which are used
public async void DisplayInt()
{
Task<int> task = new Task<int>(
() => 10);
task.Start();
Console.WriteLine(await task);
}
public async void DisplayString()
{
Task<string> task = new Task<string>(
() => "ok" );
task.Start();
Console.WriteLine(await task);
}
And usually I got next results:1) 10 ok
or 2)ok 10
but sometimes I got 3)nothing
How to get exactly result from async methods via await without using task.Result or it cannot happen?