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

Can I catch an error from async without using await?

Dealing with unhandled rejected native promises (and async/await uses native promises) is a feature supported now in V8. It’s used in the latest Chrome to output debugging information when a rejected promise is unhandled; try the following at the Babel REPL: async function executor() { console.log(“execute”); } async function doStuff() { console.log(“do stuff”); throw new … Read more

Why is an “await Task.Yield()” required for Thread.CurrentPrincipal to flow correctly?

How interesting! It appears that Thread.CurrentPrincipal is based on the logical call context, not the per-thread call context. IMO this is quite unintuitive and I’d be curious to hear why it was implemented this way. In .NET 4.5., async methods interact with the logical call context so that it will more properly flow with async … Read more

My async call is returning before list is populated in forEach loop

This code Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename) async { final file = await File(filename); String contents = await file.readAsString(); User user = User.fromJson(json.decode(contents)); String name = user.NameLast + “, ” + user.NameFirst; print(name); l.add(name); } return l; } returns the list l … Read more

Wrapping ManualResetEvent as awaitable task

RegisterWaitForSingleObject will combine waits onto dedicated waiter threads, each of which can wait on multiple handles (specifically, 63 of them, which is MAXIMUM_WAIT_OBJECTS minus one for a “control” handle). So you should be able to use something like this (warning: untested): public static class WaitHandleExtensions { public static Task AsTask(this WaitHandle handle) { return AsTask(handle, … Read more