Bushuev
Bushuev

Reputation: 577

How to get exactly result from async methods via await without Task.Result?

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?

Upvotes: 4

Views: 2292

Answers (2)

Fabjan
Fabjan

Reputation: 13676

Welcome to SO...

Well, in your case for both tasks it takes more time to initialize them, then to run the method inside. That's why sometimes second task finishes before the first one (if it was initialized in shorter time). If you change your DisplayString method to :

    public static async void DisplayString()
    {
        Task<string> task = new Task<string>(
            () => { Thread.Sleep(10); return "ok"; });
        task.Start();
        Console.WriteLine(await task);
    }

As now DisplayString method now takes around 10 ms to execute It'll will always result in :

10    
ok

P.S. As pointed by Stephen Cleary you can read some basic articles about async / await. I'd also recommend to test it on WinForms projects rather then doing it with Console.

Upvotes: 1

Stephen Cleary
Stephen Cleary

Reputation: 456647

I recommend you read some introductory articles like my own async intro, and follow that up with my MSDN article on async best practices. If you just bang out code, you're going to end up with a lot of pain.

In particular, you should not use Task.Factory.StartNew, the Task constructor, nor Task.Start; and you should avoid async void (all links are to my blog where I explain in detail why).

To answer your actual question:

How to get exactly result from async methods via await without using task.Result or it cannot happen?

To get the result from a task, you can either asynchronously wait for the task to complete (via await), or you can synchronously (blocking) wait for the task to complete (via Result). There's no "other way", if that's what you're asking.

Upvotes: 6

Related Questions