async-await

FileStream.ReadAsync very slow compared to Read()

本小妞迷上赌 提交于 2021-02-07 14:27:23
问题 I have the following code to loop thru a file and read 1024 bytes at a time. The first iteration uses FileStream.Read() and the second iteration uses FileStream.ReadAsync() . private async void Button_Click(object sender, RoutedEventArgs e) { await Task.Run(() => Test()).ConfigureAwait(false); } private async Task Test() { Stopwatch sw = new Stopwatch(); sw.Start(); int readSize; int blockSize = 1024; byte[] data = new byte[blockSize]; string theFile = @"C:\test.mp4"; long totalRead = 0;

FileStream.ReadAsync very slow compared to Read()

丶灬走出姿态 提交于 2021-02-07 14:22:10
问题 I have the following code to loop thru a file and read 1024 bytes at a time. The first iteration uses FileStream.Read() and the second iteration uses FileStream.ReadAsync() . private async void Button_Click(object sender, RoutedEventArgs e) { await Task.Run(() => Test()).ConfigureAwait(false); } private async Task Test() { Stopwatch sw = new Stopwatch(); sw.Start(); int readSize; int blockSize = 1024; byte[] data = new byte[blockSize]; string theFile = @"C:\test.mp4"; long totalRead = 0;

HttpClient.PostAsJsonAsync never sees when the post is succeeding and responding

断了今生、忘了曾经 提交于 2021-02-07 11:28:36
问题 We are using an HttpClient to post json to a restful web service. In one instance, we are running into something that has us baffled. Using tools like postman, fiddler etc, we can post to an endpoint and see that it is working. When we do the same with HttpClient.PostAsJsonAsync, we can verify in the software we are posting to that it received the data just fine. However, our PostAsJsonAsync will always eventually time out rather than give us a response. We have worked with the team that

What makes `async/await` statements run sequentially vs in parallel in ES6?

我只是一个虾纸丫 提交于 2021-02-07 11:20:15
问题 I have already gone through the thread Any difference between await Promise.all() and multiple await?, so I am clear about Promise.all and multiple awaits. Still, I am not very clear about the below 2 scenarios. In Case 1 why does it execute sequentially (takes 10s) whereas in Case 2 it executes in parallel (takes 4s)? Case 1: function promiseWait(time) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(true); }, time); }); } async function test1() { var t0 = performance

Multiple Short-lived TPL Dataflows versus Single Long-Running Flow

℡╲_俬逩灬. 提交于 2021-02-07 07:25:53
问题 I'm using TPL dataflow to process items off a queue in an Azure worker role. Should I have a single long running dataflow, or spawn a new flow for every messages I receive? If an error is thrown in a block, that block will stop accepting new messages. That means if there is an exception in a block, the whole dataflow will stop processing. I need to be able to withstand exception from something like invalid queue inputs without locking my dataflow. I see one of two options: I have a start a

WinForms window changes dimensions when it encounters an async call

不打扰是莪最后的温柔 提交于 2021-02-07 07:01:23
问题 I have a WinForms project which is several years old and has been retro-fitted with async event-handlers: private async void dgvNewOrders_CellClick(object sender, DataGridViewCellEventArgs e) Inside this method is an async call: var projectTemplate = await GetProjectTemplateFile(companyId, sourceLang, targetLang); When the program runs on a normal resolution screen, it runs as expected. However, when run on a high-DPI screen the window's dimensions - as well as those of all child controls -

Shared_Preferences for Flutter Web?

佐手、 提交于 2021-02-07 06:25:05
问题 Shared_preferences (https://pub.dev/packages/shared_preferences) doesn't seem to work for Flutter for Web. I have the following function that's called when a button is pressed. getEmail() async { print("reached 1st line"); SharedPreferences prefs = await SharedPreferences.getInstance(); print("reached 2nd line"); String _confirmedEmail = prefs.getString('_confirmedEmail') ?? ""; ) It prints "reached 1st line" but not "reached 2nd line", which means the program doesn't go past the await

Load XDocument asynchronously

佐手、 提交于 2021-02-07 05:51:24
问题 I want to load large XML documents into XDocument objects. The simple synchronous approach using XDocument.Load(path, loadOptions) works great, but blocks for an uncomfortably long time in a GUI context when loading large files (particularly from network storage). I wrote this async version with the intention of improving responsiveness in document loading, particularly when loading files over the network. public static async Task<XDocument> LoadAsync(String path, LoadOptions loadOptions =

Why does this async function execute before the equivalent Promise.then chain defined before it?

人走茶凉 提交于 2021-02-07 04:56:43
问题 I have the following code: var incr = num => new Promise(resolve => { resolve(num + 1); }); var x = incr(3) .then(resp => incr(resp)) .then(resp => console.log(resp)); async function incrTwice(num) { const first = await incr(num); const twice = await incr(first); console.log(twice); } incrTwice(6); Which I believe (perhaps mistakenly) shows two equivalent ways to achieve the same functionality: first by chaining promises and second with the syntactic sugar of async/await. I would expect the

Wrapping promise in async/await

家住魔仙堡 提交于 2021-02-07 03:22:22
问题 I'm struggling a bit with async/await and returning a value from a Promise. function test () { return new Promise((resolve, reject) => { resolve('Hello') }) } async function c() { await test() } As I understood things I should be able to get a value by doing: console.log(c()) But clearly I am missing a point here as this returns a promise. Shouldn't it print "hello"? On a similar note I am unclear as to whether a callback needs to be converted to a promise before wrapping it in async/await?