What is the difference between task and thread?

In computer science terms, a Task is a future or a promise. (Some people use those two terms synonymously, some use them differently, nobody can agree on a precise definition.) Basically, a Task<T> “promises” to return you a T, but not right now honey, I’m kinda busy, why don’t you come back later? A Thread … Read more

How can I tell Moq to return a Task?

Your method doesn’t have any callbacks so there is no reason to use .CallBack(). You can simply return a Task with the desired values using .Returns() and Task.FromResult, e.g.: MyType someValue=…; mock.Setup(arg=>arg.DoSomethingAsync()) .Returns(Task.FromResult(someValue)); Update 2014-06-22 Moq 4.2 has two new extension methods to assist with this. mock.Setup(arg=>arg.DoSomethingAsync()) .ReturnsAsync(someValue); mock.Setup(arg=>arg.DoSomethingAsync()) .ThrowsAsync(new InvalidOperationException()); Update 2016-05-05 As Seth … Read more

How to safely call an async method in C# without await

If you want to get the exception “asynchronously”, you could do: MyAsyncMethod(). ContinueWith(t => Console.WriteLine(t.Exception), TaskContinuationOptions.OnlyOnFaulted); This will allow you to deal with an exception on a thread other than the “main” thread. This means you don’t have to “wait” for the call to MyAsyncMethod() from the thread that calls MyAsyncMethod; but, still allows you … Read more

WaitAll vs WhenAll

Task.WaitAll blocks the current thread until everything has completed. Task.WhenAll returns a task which represents the action of waiting until everything has completed. That means that from an async method, you can use: await Task.WhenAll(tasks); … which means your method will continue when everything’s completed, but you won’t tie up a thread to just hang … Read more

Asynchronously wait for Task to complete with timeout

How about this: int timeout = 1000; var task = SomeOperationAsync(); if (await Task.WhenAny(task, Task.Delay(timeout)) == task) { // task completed within timeout } else { // timeout logic } And here’s a great blog post “Crafting a Task.TimeoutAfter Method” (from MS Parallel Library team) with more info on this sort of thing. Addition: at … Read more

If my interface must return Task what is the best way to have a no-operation implementation?

Today, I would recommend using Task.CompletedTask to accomplish this. Pre .net 4.6: Using Task.FromResult(0) or Task.FromResult<object>(null) will incur less overhead than creating a Task with a no-op expression. When creating a Task with a result pre-determined, there is no scheduling overhead involved.

Best practice to call ConfigureAwait for all server-side code

Update: ASP.NET Core does not have a SynchronizationContext. If you are on ASP.NET Core, it does not matter whether you use ConfigureAwait(false) or not. For ASP.NET “Full” or “Classic” or whatever, the rest of this answer still applies. Original post (for non-Core ASP.NET): This video by the ASP.NET team has the best information on using … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)