0

I have a stack of calls to a web api that need to be called asynchronously. I have never used the aysnc.

I have created a simple test console program, along the lines of :

class Program
{
    static void Main(string[] args)
    {
        ClassOne xx = new ClassOne();
        var v1 = xx.DummyTask();
    }
}

With the class defined as :

namespace GHTest.classes
{
    public class ClassOne
    {
        GitHubClient client;

        public ClassOne()
        {
           client = new GitHubClient(new ProductHeaderValue("OMHSite"));
        }

        public async Task<string> DummyTask()
        {
            Task<Repository> task = client.Repository.Get("openEHR", "CKM-mirror");
            task.Wait();

            var myResult = task.Result;
            return myResult.FullName;
        }
    }
}

Visual Studio states I should use the "await" operator as currently this code will run synchronously. Where does the await operator go?

Furthermore if the following statement throws an exception, how do I catch that in the task

client.Repository.Get("openEHR", "CKM-mirror");
GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
BENBUN Coder
  • 4,801
  • 7
  • 52
  • 89
  • Assuming you've searched for something like https://www.bing.com/search?q=c%23+async+console your code looks strange... Please update your post with links you've used to learn about `async`/`await` usage in console apps so it is easier to understand what you don't know/need help with. – Alexei Levenkov May 16 '15 at 00:22

1 Answers1

6

task.Wait(); is redudant, the call to task.Result would wait implicitly.

Here is your method rewritten to use await.

Task<Repository> task = client.Repository.Get("openEHR", "CKM-mirror");
var myResult = await task;

return myResult.FullName;

You don't need .Result either as the type of await on a Task<T> is T.

On the subject of exception handling you will need to do a try/catch around the await (or Result in your original code). As that is when the exception will be rethrown. Note that this is only true if the exception is thrown in the Task, in theory the Get function itself could throw which would have to be caught there.

Also note that since you are returning an async you can choose to catch the exception wherever you await (or Result) the call.

Finally don't forget that if you don't await the result of DummyTask your task may not complete (or Result).

static void Main(string[] args)
{
    ClassOne xx = new ClassOne();
    var v1 = xx.DummyTask();
    var resultV1 = v1.Result; //Forces the execution of v1 by requesting its result.
}
Guvante
  • 18,775
  • 1
  • 33
  • 64
  • I changed the code as suggested removing the task.Wait(); When the calling programs gets the response from the method the task is still waiting for activation, even though the "await" statement has been added. If I add the task.Wait(); back again then it works.... – BENBUN Coder May 15 '15 at 23:37
  • 2
    @BENBUNCoder You also need to `await` or `Wait()` the result of `DummyTask()`; that is, in your `static void Main(string[] args)` you need to do: `ClassOne xx = new ClassOne(); Task task = xx.DummyTask(); task.Wait(); var v1 = task.Result; Console.WriteLine(v1);`. – Der Kommissar May 16 '15 at 02:42