async-await

Task.Factory.StartNew with async lambda and Task.WaitAll

寵の児 提交于 2020-01-02 23:50:31
问题 I'm trying to use Task.WaitAll on a list of tasks. The thing is the tasks are an async lambda which breaks Tasks.WaitAll as it never waits. Here is an example code block: List<Task> tasks = new List<Task>(); tasks.Add(Task.Factory.StartNew(async () => { using (dbContext = new DatabaseContext()) { var records = await dbContext.Where(r => r.Id = 100).ToListAsync(); //do long cpu process here... } } Task.WaitAll(tasks); //do more stuff here This doesn't wait because of the async lambda. So how

How to correct write test with async methods?

末鹿安然 提交于 2020-01-02 14:33:27
问题 I have class for iterating images. public class PictureManager { private int _current; public List<BitmapImage> _images = new List<BitmapImage>(5); public static string ImagePath = "dataImages"; public async void LoadImages() { _images = await GetImagesAsync(); } public async Task<List<BitmapImage>> GetImagesAsync() { var files = new List<BitmapImage>(); StorageFolder picturesFolder = await KnownFolders.PicturesLibrary.GetFolderAsync("dataImages"); IReadOnlyList<IStorageItem> itemsList =

How to correct write test with async methods?

筅森魡賤 提交于 2020-01-02 14:32:02
问题 I have class for iterating images. public class PictureManager { private int _current; public List<BitmapImage> _images = new List<BitmapImage>(5); public static string ImagePath = "dataImages"; public async void LoadImages() { _images = await GetImagesAsync(); } public async Task<List<BitmapImage>> GetImagesAsync() { var files = new List<BitmapImage>(); StorageFolder picturesFolder = await KnownFolders.PicturesLibrary.GetFolderAsync("dataImages"); IReadOnlyList<IStorageItem> itemsList =

How to correct write test with async methods?

我与影子孤独终老i 提交于 2020-01-02 14:31:45
问题 I have class for iterating images. public class PictureManager { private int _current; public List<BitmapImage> _images = new List<BitmapImage>(5); public static string ImagePath = "dataImages"; public async void LoadImages() { _images = await GetImagesAsync(); } public async Task<List<BitmapImage>> GetImagesAsync() { var files = new List<BitmapImage>(); StorageFolder picturesFolder = await KnownFolders.PicturesLibrary.GetFolderAsync("dataImages"); IReadOnlyList<IStorageItem> itemsList =

async/await thread transition curiosity

只愿长相守 提交于 2020-01-02 12:24:51
问题 I have the following simple console application: class Program { private static int times = 0; static void Main(string[] args) { Console.WriteLine("Start {0}", Thread.CurrentThread.ManagedThreadId); var task = DoSomething(); task.Wait(); Console.WriteLine("End {0}", Thread.CurrentThread.ManagedThreadId); Console.ReadLine(); } static async Task<bool> DoSomething() { times++; if (times >= 3) { return true; } Console.WriteLine("DoSomething-1 sleeping {0}", Thread.CurrentThread.ManagedThreadId);

Async/Await - How to implement Javascript Async-Await in a recursive Ajax function?

一世执手 提交于 2020-01-02 10:16:15
问题 I have two functions. I call trendyolStocksUpdate() function several times with a loop inside syncTrendyolOFFStocks() function. I used async/await but trendyolStocksUpdate() function is not called sequentially. It runs simultaneously. What is wrong with my code ? Below are the two functions: async function syncTrendyolOFFStocks(){ //This function sends "out of stock products" one by one //to trendyolStocksUpdate function, so they their quantity will be initalized to 0 //For example, T-Shirt

Why sync context is not working for await?

与世无争的帅哥 提交于 2020-01-02 08:16:24
问题 This answer says by default the await operator will capture the current "context" and use that to resume the async method. I am trying this code in my console app: static void Main(string[] args) { Test().Wait(); } private static async Task Test() { var context = new SynchronizationContext(); SynchronizationContext.SetSynchronizationContext(context); Console.WriteLine("Thread before: " + Thread.CurrentThread.ManagedThreadId); Console.WriteLine(await GetResultAsync()); Console.WriteLine(

Angular 1.5 && Async/Await && jasmine tests

心已入冬 提交于 2020-01-02 07:58:52
问题 I already looked everywhere but could not find a solution yet for my particular case. We are using angular 1.5 and a Karma/Jasmine setup for unit tests. In the initial source code, I used ES2017 async/await in the controller. That seemed to work fine as long as I added $apply of $digest manually at the end. So for example: async function loadData() { try { vm.isLoading = true; vm.data = await DataService.getData(); $scope.$apply(); } catch (ex) { vm.isLoading = false; } } To write an

How can I abort an async-await function after a certain time?

╄→尐↘猪︶ㄣ 提交于 2020-01-02 06:58:35
问题 For example, the following is an async function: async function encryptPwd() { const salt = await bcrypt.genSalt(5); const encryptedPwd = await bcrypt.hash(password, salt); return encryptedPwd; } If the server is lagging a lot, I want to abort this activity and return an error. How can I set a timeout for like 10 sec (for example)? 回答1: You could wrap the hash function in another promise. function hashWithTimeout(password, salt, timeout) { return new Promise(function(resolve, reject) { bcrypt

Async WCF call to save thread?

南笙酒味 提交于 2020-01-02 06:45:07
问题 In another SO question, I was advised to send an asynchronous network request, rather than sending a synchronous request on a background thread. The reason was so that I don't waste a thread. I'm trying to understand how this is so. This is the original approach. I can understand how there are two threads here. One is the main thread (1), and one is the background thread (Task.Run) (2) that makes the WCF call: This is my sketch of the suggested approach. I'm trying to understand how a thread