continuewith

C# Chained ContinueWith Not Waiting for Previous Task to Complete

半城伤御伤魂 提交于 2020-07-21 03:12:36
问题 I am testing the asynchronousity of C# async/await and came across a surprise where the subsequent code for ContinueWith does not wait for the previous task to complete: public async Task<int> SampleAsyncMethodAsync(int number,string id) { Console.WriteLine($"Started work for {id}.{number}"); ConcurrentBag<int> abc = new ConcurrentBag<int>(); await Task.Run(() => { for (int count = 0; count < 30; count++) { Console.WriteLine($"[{id}] Run: {number}"); abc.Add(count); } }); Console.WriteLine($

C# Chained ContinueWith Not Waiting for Previous Task to Complete

北慕城南 提交于 2020-07-21 03:12:08
问题 I am testing the asynchronousity of C# async/await and came across a surprise where the subsequent code for ContinueWith does not wait for the previous task to complete: public async Task<int> SampleAsyncMethodAsync(int number,string id) { Console.WriteLine($"Started work for {id}.{number}"); ConcurrentBag<int> abc = new ConcurrentBag<int>(); await Task.Run(() => { for (int count = 0; count < 30; count++) { Console.WriteLine($"[{id}] Run: {number}"); abc.Add(count); } }); Console.WriteLine($

How to do something on

回眸只為那壹抹淺笑 提交于 2020-01-17 15:49:11
问题 Public Shared Async Function getMarketDetailFromAllExchangesAsync() As Task Dim taskList = New List(Of Task) For Each account In uniqueAccounts() Dim newtask = account.Value.getMarketInfoAsync() taskList.Add(newtask) Next Await Task.WhenAll(taskList.ToArray) Dim b = 1 End Function The code work just fine. However, I want to log every time a task is done So I did newtask.ContinueWith(Async Function(x) LogEvents(account.ToString)) LogEvents is a normal function. I got 2 error How exactly should

Async await and continuewith for a Entity Framework database Process Method in C#

痴心易碎 提交于 2019-12-12 04:13:43
问题 I have a Save method to add/update a contact. I designed an asynchronous method. But I'm not able to get the records from the database. Kindly have a look at the code: public async Task<bool> SaveContact(Contact contact) { bool flag = false; try { if(contact != null) { using(var dbContext = DBContext()) { ContactEDB contactObj = new ContactEDB(); if(contact.Id > 0) { contactObj = await dbContext.Contact.FirstOrDefaultAsync(a => a.Id == contact.Id); // The local variable "contactObj" always

It takes more than a few seconds for a task to start running

馋奶兔 提交于 2019-12-11 17:42:41
问题 I'm developing an application using WPF and C# . I have the following code: var tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; Task task = Task.Factory.StartNew(() => { // Some action that returns a boolean - **CODE_A** }).ContinueWith((task2) => { result= task2.Result; if (!result) { //Another action **CODE_B** } }); }, token); Usually CODE_A starts running immediately, and after less than a second, CODE_B starts executing. But, sometimes it takes

Is reassigning a task value necessary when not using task.ContinueWith()

半世苍凉 提交于 2019-12-11 16:44:30
问题 When reading a post, Starting Tasks In foreach Loop Uses Value of Last Item), the marked answer makes a lot of sense. The author created a new variable, pathCopy, to use in the task. My question is, is this only necessary when Task.ContinueWith() is used? Here is an example: private void GetAuditFiles() { _auditFiles = new ConcurrentBag<AuditFile>(); var tasks = new List<Task>(); foreach (var auditFile in Directory.GetFiles(_properties.AuditFileOutputPath)) { var taskfile = auditFile; tasks