await vs Task.Wait – Deadlock?

I don’t quite understand the difference between Task.Wait and await. I have something similar to the following functions in a ASP.NET WebAPI service: public class TestController : ApiController { public static async Task<string> Foo() { await Task.Delay(1).ConfigureAwait(false); return “”; } public async static Task<string> Bar() { return await Foo(); } public async static Task<string> Ros() … Read more

What is the difference between Task.Run() and Task.Factory.StartNew()

I have Method : private static void Method() { Console.WriteLine(“Method() started”); for (var i = 0; i < 20; i++) { Console.WriteLine(“Method() Counter = ” + i); Thread.Sleep(500); } Console.WriteLine(“Method() finished”); } And I want to start this method in a new Task. I can start new task like this var task = Task.Factory.StartNew(new Action(Method)); … Read more

Calling async method synchronously

I have an async method: public async Task<string> GenerateCodeAsync() { string code = await GenerateCodeService.GenerateCodeAsync(); return code; } I need to call this method from a synchronous method. How can I do this without having to duplicate the GenerateCodeAsync method in order for this to work synchronously? Update Yet no reasonable solution found. However, I … Read more

Catch an exception thrown by an async void method

Using the async CTP from Microsoft for .NET, is it possible to catch an exception thrown by an async method in the calling method? public async void Foo() { var x = await DoSomethingAsync(); /* Handle the result, but sometimes an exception might be thrown. For example, DoSomethingAsync gets data from the network and the … Read more

Running multiple async tasks and waiting for them all to complete

I need to run multiple async tasks in a console application, and wait for them all to complete before further processing. There’s many articles out there, but I seem to get more confused the more I read. I’ve read and understand the basic principles of the Task library, but I’m clearly missing a link somewhere. … Read more

Synchronously waiting for an async operation, and why does Wait() freeze the program here

Preface: I’m looking for an explanation, not just a solution. I already know the solution. Despite having spent several days studying MSDN articles about the Task-based Asynchronous Pattern (TAP), async and await, I’m still a bit confused about some of the finer details. I’m writing a logger for Windows Store Apps, and I want to … Read more

How can I tell Moq to return a Task?

I’ve got an interface which declares Task DoSomethingAsync(); I’m using MoqFramework for my tests: [TestMethod()] public async Task MyAsyncTest() { Mock<ISomeInterface> mock = new Mock<ISomeInterface>(); mock.Setup(arg => arg.DoSomethingAsync()).Callback(() => { <my code here> }); … } Then in my test I execute the code which invokes await DoSomethingAsync(). And the test just fails on that … Read more

What is the difference between task and thread?

In C# 4.0, we have Task in the System.Threading.Tasks namespace. What is the true difference between Thread and Task. I did some sample program(help taken from MSDN) for my own sake of learning with Parallel.Invoke Parallel.For Parallel.ForEach but have many doubts as the idea is not so clear. I have initially searched in Stackoverflow for … Read more