async-await

Asynchronous downloading files in C#

六眼飞鱼酱① 提交于 2019-12-23 01:49:06
问题 I have a question about asynchronous operations in C#. Suppose I have some code like this: public async void Download(string[] urls) { for(int i = 0; i < urls.Length; i++); { await DownloadHelper.DownloadAsync(urls[i], @"C:\" + i.ToString() + ".mp3"); } } But this code does not really download files asynchronously. It begins to download the file from the first URL and then awaits this operation. It then begins to download the file from the second URL... and so on. Thereby files are downloaded

Control total number of tasks when using Async/Await in a recursive function

一个人想着一个人 提交于 2019-12-23 01:41:18
问题 I have written this code. It recursively creates folders in the web system by making REST Calls. So basically, it creates a folder for the root node, then gets all the child nodes and parallely and recursively calls itself. (for each child) the only problem with the code is that if a node has too may children OR if the hierarchy is too deep, then I start getting "TaskCancellation" errors. I have already tried increasing the timeout to 10 minutes.. but that does not solve the problem. So my

Exception: The application called an interface that was marshalled for a different thread

筅森魡賤 提交于 2019-12-22 18:17:41
问题 private void LogInButton_Click(object sender, RoutedEventArgs e) { var api = new RestAPI("http://localhost:2624/", UsernameTextBox.Text, PasswordTextBox.Password); api.AutenticarUsuarioFinalizado += (o, args) => { ProgressBar.IsIndeterminate = false; ProgressBar.Visibility = Visibility.Collapsed; LogInButton.IsEnabled = true; if (args.Error) return; if (args.Resultado.Autenticado) { } }; api.AutenticarUsuario(); ProgressBar.Visibility = Visibility.Visible; ProgressBar.IsIndeterminate = true;

WhenAll vs WaitAll in parallel

旧时模样 提交于 2019-12-22 17:39:40
问题 I'm trying to understand how WaitAll and WhenAll works and have following problem. There are two possible ways to get a result from a method: return Task.WhenAll(tasks).Result.SelectMany(r=> r); return tasks.Select(t => t.Result).SelectMany(r => r).ToArray(); If I understand correctly, the second case is like calling WaitAll on tasks and fetching the results after that. It looks like the second case has much better performance. I know that the proper usage of WhenAll is with await keyword,

Strange appearance of a null entry in the list of tasks

浪尽此生 提交于 2019-12-22 15:05:31
问题 Here is the code involved: private static async Task DoRunInOrderAsync<TTaskSeed>(SemaphoreSlim sem, IObservable<TTaskSeed> taskSeedSource, CreateTaskDelegate<TTaskSeed> createTask, OnTaskErrorDelegate<TTaskSeed> onFailed, OnTaskSuccessDelegate<TTaskSeed> onSuccess) where TTaskSeed : class { var tasks = await taskSeedSource .Select(taskSeed => GetPendingOrRunningTask(taskSeed, createTask, onFailed, onSuccess, sem)) .ToList() .ToTask(); await Task.WhenAll(tasks); } private static async Task

Strange appearance of a null entry in the list of tasks

狂风中的少年 提交于 2019-12-22 15:05:09
问题 Here is the code involved: private static async Task DoRunInOrderAsync<TTaskSeed>(SemaphoreSlim sem, IObservable<TTaskSeed> taskSeedSource, CreateTaskDelegate<TTaskSeed> createTask, OnTaskErrorDelegate<TTaskSeed> onFailed, OnTaskSuccessDelegate<TTaskSeed> onSuccess) where TTaskSeed : class { var tasks = await taskSeedSource .Select(taskSeed => GetPendingOrRunningTask(taskSeed, createTask, onFailed, onSuccess, sem)) .ToList() .ToTask(); await Task.WhenAll(tasks); } private static async Task

Deadlock reading async response content from async DelegatingHandler in webapi

百般思念 提交于 2019-12-22 13:59:11
问题 Problem I'm using a DelegatingHandler in WebAPI to read the response Content and audit it to the database. When the code gets to .ReadAsStringAsync() it appears to lock and prevent other requests from completing. I only know this, because when I remove the offending line, it works perfectly. public static void Register(HttpConfiguration config) { config.MessageHandlers.Add(new ResourceAuditHandler()); } public class ResourceAuditHandler : DelegatingHandler { protected override async Task

Non blocking and reoccurring producer/consumer notifier implementation

梦想与她 提交于 2019-12-22 13:06:35
问题 Searched hard for a piece of code which does what i want and i am happy with. Reading this and this helped a lot. I have a scenario where i need a single consumer to be notified by a single producer when new data is available but would also like the consumer to be notified periodically regardless of if new data is available. It is fine if the consumer is notified more than the reoccurring period but it should not be notified less frequent. It is possible that multiple notifications for 'new

C# Webservice: Webmethod, that calls an async method, returns Taskoff object

寵の児 提交于 2019-12-22 12:19:12
问题 I'm a total beginner with webservices and I ran in the following problem, which is about a webservice and asynchronous logging. (c#) Problem Description I have a webservice that returns some data to a client (asp.net website). [WebMethod] public MyClass getData() { //Do some work return _myClassObject; } This works pretty good so far. Because I want to know what is happening on the webservice once it is published, I tried to implement some simple logging. The following class (simplified)

concat result of three tasks in an async method in a proper way

北慕城南 提交于 2019-12-22 11:19:36
问题 I am very new in async programming. I was wonder if there is any way for these tasks to run simultaneously. Consider I have this code: public async Task<IList<WebData>> GetAllAsync() { var twitterData = await ProvidetwitterDataAsync(); var facebookData = await ProvidefacebookDataAsync(); var linkedinData = await ProvidelinkedinDataAsync(); return twitterData .Concat(facebookData ).Concat(linkedinData ).ToList(); } I think because I used await twitterData that Task should be done and after