Task.Delay() not behaving as expected

Task.Delay returns a Task object on which you have to wait. Otherwise the following code will be executed immediately. Therfore you have to declare your method as async. Then you can await Task.Delay public async Task ChattyWriter() { int count = 0; while (true) { var message = String.Format(“Chatty Writer number {0}”, count); Trace.WriteLine(message); count++; … Read more

How to use non-thread-safe async/await APIs and patterns with ASP.NET Web API?

Entity Framework will (should) handle thread jumps across await points just fine; if it doesn’t, then that’s a bug in EF. OTOH, OperationContextScope is based on TLS and is not await-safe. 1. Synchronous APIs maintain your ASP.NET context; this includes things such as user identity and culture that are often important during processing. Also, a … Read more

Specifying a Thread’s Name when using Task.StartNew

Well, this works: class Program { static void Main(string[] args) { var task = Task.Factory.StartNew(() => { Thread.CurrentThread.Name = “foo”; Thread.Sleep(10000); // Use Debug + Break to see it }); task.Wait(); } } There’s a problem however, the threadpool thread gets recycled and won’t change its name. This can be confusing, you’ll see it running … Read more

List thread safety

No! It is not safe at all, because processed.Add is not. You can do following: items.AsParallel().Select(item => SomeProcessingFunc(item)).ToList(); Keep in mind that Parallel.ForEach was created mostly for imperative operations for each element of sequence. What you do is map: project each value of sequence. That is what Select was created for. AsParallel scales it across … Read more

TPL Dataflow, guarantee completion only when ALL source data blocks completed

The issue is exactly what casperOne said in his answer. Once the first transform block completes, the processor block goes into “finishing mode”: it will process remaining items in its input queue, but it won’t accept any new items. There is a simpler fix than splitting your processor block in two though: don’t set PropagateCompletion, … Read more

Long running task in WebAPI

Great news, there is a new solution in .NET 4.5.2 called the QueueBackgroundWorkItem API. It’s really simple to use: HostingEnvironment.QueueBackgroundWorkItem(ct => DoSomething(a, b, c)); Here’s an article that describes it in detail. https://blogs.msdn.microsoft.com/webdev/2014/06/04/queuebackgroundworkitem-to-reliably-schedule-and-run-background-processes-in-asp-net/ And here’s anohter article that mentions a few other approaches not mentioned in this thread. http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx

Should we use ConfigureAwait(false) in libraries that call async callbacks?

When you say await task.ConfigureAwait(false) you transition to the thread-pool causing mapping to run under a null context as opposed to running under the previous context. That can cause different behavior. So if the caller wrote: await Map(0, i => { myTextBox.Text = i.ToString(); return 0; }); //contrived… Then this would crash under the following … Read more

Task.FromResult() vs. Task.Run()

If your method is synchronous you shouldn’t return a Task to begin with. Just create a traditional synchronous method. If for some reason that’s not possible (for example, you implement some async interface) returning a completed task using Task.FromResult or even better in this case Task.CompletedTask (added in .NET 4.6) is much better than using … Read more

Calling TaskCompletionSource.SetResult in a non blocking manner

I’ve discovered that TaskCompletionSource.SetResult(); invokes the code awaiting the task before returning. In my case that result in a deadlock. Yes, I have a blog post documenting this (AFAIK it’s not documented on MSDN). The deadlock happens because of two things: There’s a mixture of async and blocking code (i.e., an async method is calling … Read more

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