async-await

Async Program still freezing up the UI

随声附和 提交于 2019-12-18 09:39:36
问题 Hello I'm writing a WPF program that gets has thumbnails inside a ThumbnailViewer. I want to generate the Thumbnails first, then asynchronously generate the images for each thumbnail. I can't include everything but I think this is whats relevant Method to generate the thumbnails. public async void GenerateThumbnails() { // In short there is 120 thumbnails I will load. string path = @"C:\....\...\...png"; int pageCount = 120; SetThumbnails(path, pageCount); await Task.Run(() => GetImages(path,

Asynchrony and thread culture

前提是你 提交于 2019-12-18 08:10:32
问题 I have an MVC app where I override my base controller's OnActionExecuting() method to set my thread culture: protected override void OnActionExecuting(ActionExecutingContext filterContext) { var langCode = GetLangCode(); Thread.CurrentThread.CurrentUICulture = new CultureInfo(langCode); Thread.CurrentThread.CurrentCulture = new CultureInfo(langCode); } As I have started to program asynchronously more, I'm curious about how culture is persisted if we return the thread whose culture we've

Why does “await LoadAsync()” freeze the UI while “await Task.Run(() => Load())” does not?

折月煮酒 提交于 2019-12-18 07:12:26
问题 I'm following a walkthrough on how to combine EntityFramework with WPF. I decided to play around with async/await while I'm at it, because I've never really had a chance to use it before (we've just moved to VS2013/.NET 4.5 from VS2010/.NET 4.0). Getting the save button handler to be async was a breeze, and the UI remained responsive (I can drag the window around) while SaveChangesAsync() is awaited. In the window load handler, however, I ran into a small snag. private async void Window

ContextBoundObject Throws a Remoting Error After Await

落爺英雄遲暮 提交于 2019-12-18 07:00:00
问题 I have some logging code that was written to intercept method calls using ContextBoundObject s and a ContextAttribute. The code is based on a Code Project sample. This all worked fine until we started using this library with code that leverages async and await. Now we get remoting errors when running the code. Here is a simple example that reproduces the issue: public class OhMyAttribute : ContextAttribute { public OhMyAttribute() : base("OhMy") { } } [OhMy] public class Class1 :

Await multiple async Task while setting max running task at a time

蹲街弑〆低调 提交于 2019-12-18 06:54:14
问题 So I just started to try and understand async, Task, lambda and so on, and I am unable to get it to work like I want. With the code below I want for it to lock btnDoWebRequest, do a unknow number of WebRequests as a Task and once all the Task are done unlock btnDoWebRequest. However I only want a max of 3 or whatever number I set of Tasks running at one time, which I got partly from Have a set of Tasks with only X running at a time. But after trying and modifying my code in multiple ways, it

Task constructor vs Task.Run with async Action - different behavior

最后都变了- 提交于 2019-12-18 06:53:51
问题 Could anyone please explain this, perhaps I'm missing something obvious. These 2 cases seem to be identical in behavior, and yet they are not. Case 1 : Start a Task with an async Action, that does some work for some time: var t = Task.Run(async () => { await Task.Delay(2000); }); A second task waits for the first one: var waitingTask = Task.Run(() => { t.Wait(); }); Wait for the second task: waitingTask.Wait(); Case 2 : Build a Task using the Task constructor, passing the same async Action :

What's the correct way to run multiple parallel tasks in an asp.net process?

牧云@^-^@ 提交于 2019-12-18 06:48:45
问题 I think I'm not understanding something. I had thought that Task.Yield() forced a new thread/context to be started for a task but upon re-reading this answer it seems that it merely forces the method to be async. It will still be on the same context. What's the correct way - in an asp.net process - to create and run multiple tasks in parallel without causing deadlock? In other words, suppose I have the following method: async Task createFileFromLongRunningComputation(int input) { //many

Will every 'await' operator result in a state machine?

佐手、 提交于 2019-12-18 06:35:11
问题 Please consider the following code: public async Task<string> GetString() { //Some code here... var data = await A(); //Some more code... return data; } private async Task<string> A() { //Some code here.. var data = await B(); //manipulating data... return data; } private async Task<string> B() { //Some code here.. var data = await C(); //manipulating data... return data; } private async Task<string> C() { //Some code here.. var data = await FetchFromDB(); //manipulating data... return data;

Will every 'await' operator result in a state machine?

↘锁芯ラ 提交于 2019-12-18 06:34:32
问题 Please consider the following code: public async Task<string> GetString() { //Some code here... var data = await A(); //Some more code... return data; } private async Task<string> A() { //Some code here.. var data = await B(); //manipulating data... return data; } private async Task<string> B() { //Some code here.. var data = await C(); //manipulating data... return data; } private async Task<string> C() { //Some code here.. var data = await FetchFromDB(); //manipulating data... return data;

Exception handling inside “async void” WPF command handlers

痴心易碎 提交于 2019-12-18 05:54:09
问题 I'm reviewing some WPF code of my colleagues, which is a library of UserControl -based components with a lot of async void event and command handlers. These methods currently do not implement any error handling internally. The code in a nutshell: <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed"/> </Window.CommandBindings> private async void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e) { // do some fake async work (and