c#-5.0

ICommandHandler/IQueryHandler with async/await

筅森魡賤 提交于 2019-11-29 21:06:18
EDITH says (tl;dr) I went with a variant of the suggested solution; keeping all ICommandHandler s and IQueryHandler s potentially aynchronous and returning a resolved task in synchronous cases. Still, I don't want to use Task.FromResult(...) all over the place so I defined an extension method for convenience: public static class TaskExtensions { public static Task<TResult> AsTaskResult<TResult>(this TResult result) { // Or TaskEx.FromResult if you're targeting .NET4.0 // with the Microsoft.BCL.Async package return Task.FromResult(result); } } // Usage in code ... using TaskExtensions; class

Debugging exceptions in a Async/Await (Call Stack)

偶尔善良 提交于 2019-11-29 20:51:50
I use the Async/Await to free my UI-Thread and accomplish multithreading. Now I have a problem when I hit a exception. The Call Stack of my Async parts allways starts with ThreadPoolWorkQue.Dipatch() , which doesn't help me very much. I found a MSDN-Article Andrew Stasyuk. Async Causality Chain Tracking about it but as I understand it, its not a ready to use solution. What is the best/easiest way to debug if you use multithreading with Async/Await? The article you found does a good job of explaining why call stacks don't work the way most of us think they do. Technically, the call stack only

What are the differences between using ConfigureAwait(false) and Task.Run?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 19:51:24
I understand that it's recommended to use ConfigureAwait(false) for await s in library code so that subsequent code does not run in the caller's execution context, which could be a UI thread. I also understand that await Task.Run(CpuBoundWork) should be used instead of CpuBoundWork() for the same reason. Example with ConfigureAwait public async Task<HtmlDocument> LoadPage(Uri address) { using (var client = new HttpClient()) using (var httpResponse = await client.GetAsync(address).ConfigureAwait(false)) using (var responseContent = httpResponse.Content) using (var contentStream = await

Cold Tasks and TaskExtensions.Unwrap

江枫思渺然 提交于 2019-11-29 15:28:16
问题 I've got a caching class that uses cold (unstarted) tasks to avoid running the expensive thing multiple times. public class AsyncConcurrentDictionary<TKey, TValue> : System.Collections.Concurrent.ConcurrentDictionary<TKey, Task<TValue>> { internal Task<TValue> GetOrAddAsync(TKey key, Task<TValue> newTask) { var cachedTask = base.GetOrAdd(key, newTask); if (cachedTask == newTask && cachedTask.Status == TaskStatus.Created) // We won! our task is now the cached task, so run it cachedTask.Start()

How to measure performance of awaiting asynchronous operations?

谁说我不能喝 提交于 2019-11-29 13:43:59
I have a Windows Service that reads from multiple MessageQueue instances. Those messagequeues all run their own Task for reading messages. Normally, after reading a message, the work of an I/O database is done. I've found articles claiming it's a good idea to use async on I/O operations, because it would free up threads. I'm trying to simulate the performance boost of using async I/O opertations in a Console application. The Console application In my test environment, I have 10 queues. GetQueues() returns 10 different MessageQueue instances. static void Main(string[] args) { var isAsync =

Why InvalidCastException when awaiting Task-returning method?

有些话、适合烂在心里 提交于 2019-11-29 11:47:15
(The real title of the question should be "Why do I get a 'Unable to cast object of type 'System.Runtime.CompilerServices.TaskAwaiter`1[System.Runtime.CompilerServices.VoidTaskResult]' to type 'System.Runtime.CompilerServices.INotifyCompletion'", but unfortunately this is too long for StackOverflow. :) Hi, I'm getting really peculiar problems when trying to await the execution of a method of mine. The calling code looks like this (excerpt): private async Task DownloadAddonFileAsync(dynamic addon, dynamic file, string targetFolder) { // ... await DownloadFileAsync(file, targetFolder, uri); The

Asynchronous Programming with Async and Await

妖精的绣舞 提交于 2019-11-29 09:47:03
I'm walking through this tutorial on how to program asynchronously in c# and have come across an error I'm not sure how to resolve. Here's the link: http://msdn.microsoft.com/en-us/library/hh191443.aspx and the error is: Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly? I am targeting the .NET 4.0 framework and am unsure as to any additional assemblies required. Here is the code: public async Task<string> AccessTheWebAsync(Class1 class1, Class2 class2) { // GetStringAsync returns a Task<string>. That

Limit number of Threads in Task Parallel Library

大城市里の小女人 提交于 2019-11-29 09:35:39
问题 I have few hundreds of files i need to upload to Azure Blob Storage. I want to use parallel task library. But instead of running all the 100 threads to upload in a foreach on list of files, how can i put a limit on max number of threads that it can use and finish the job in parallel. or does it balance the things automatically? 回答1: You should not be using threads for this at all. There's a Task -based API for this, which is naturally asynchronous: CloudBlockBlob.UploadFromFileAsync. Use it

Unnecessary async/await when await is last?

99封情书 提交于 2019-11-29 09:17:30
I've been dealing quite a lot with async await lately (read every possible article including Stephen's and Jon's last 2 chapters) , but I have come to conclusion and I don't know if it's 100% correct. - hence my question . Since async only allows the word await to be present , i'll leave async aside. AFAIU , await is all about continuation . instead of writing functional (continuational) code , write synchronous code. ( i like to refer it as callback'able code) So when the compiler reaches await - it splits the code to 2 sections and registers the second part to be executed after the first

await Task.WhenAll() vs Task.WhenAll().Wait()

两盒软妹~` 提交于 2019-11-29 05:46:33
问题 I have a method that produces an array of tasks (See my previous post about threading) and at the end of this method I have the following options: await Task.WhenAll(tasks); // done in a method marked with async Task.WhenAll(tasks).Wait(); // done in any type of method Task.WaitAll(tasks); Basically I am wanting to know what the difference between the two whenall s are as the first one doesn't seem to wait until tasks are completed where as the second one does, but I'm not wanting to use the