async-await

Faulted vs Canceled task status after CancellationToken.ThrowIfCancellationRequested

北慕城南 提交于 2019-12-30 02:45:11
问题 Usually I don't post a question with the answer, but this time I'd like to attract some attention to what I think might be an obscure yet common issue. It was triggered by this question, since then I reviewed my own old code and found some of it was affected by this, too. The code below starts and awaits two tasks, task1 and task2 , which are almost identical. task1 is only different from task2 in that it runs a never-ending loop. IMO, both cases are quite typical for some real-life scenarios

Getting return value from Task.Run

淺唱寂寞╮ 提交于 2019-12-30 01:35:09
问题 I have the following code: public static async Task<string> Start(IProgress<ProcessTaskAsyncExProgress> progress) { const int total = 10; for (var i = 0; i <= total; i++) { await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))); if (progress != null) { var args = new ProcessTaskAsyncExProgress { ProgressPercentage = (int)(i / (double)total * 100.0), Text = "processing " + i }; progress.Report(args); } } return "Done"; } private static string RunLongTask(string taskName) {

What are the differences between using ConfigureAwait(false) and Task.Run?

社会主义新天地 提交于 2019-12-29 10:14:45
问题 I understand that it's recommended to use ConfigureAwait(false) for await s in library code so that subsequent code does not run in the caller's execution context, which could be a UI thread. I also understand that await Task.Run(CpuBoundWork) should be used instead of CpuBoundWork() for the same reason. Example with ConfigureAwait public async Task<HtmlDocument> LoadPage(Uri address) { using (var client = new HttpClient()) using (var httpResponse = await client.GetAsync(address)

Using async await on custom promise

青春壹個敷衍的年華 提交于 2019-12-29 08:57:08
问题 Im trying to use async await on a function that returns a promise but the out put im getting is Promise { <pending> } . In here im using function called convertFiletoPDF which returns a promise. I need to get the output (the path that i have mention in resolve() ). When i use it as convertFiletoPDF(file).then((result) => { console.log(result); }).catch((err)=>{ console.log(err); }); it gives the expected result.Whats wrong with the code below? im quite new to these async await and promises.

Getting value from returned promise from async function

吃可爱长大的小学妹 提交于 2019-12-29 08:39:06
问题 I'm getting used to the proposed async/await syntax and there's some unintuitive behavior. Inside the "async" function, I can console.log the correct string. However, when I try to return that string, instead, it returns a promise. Checking this entry: async/await implicitly returns promise? , it's pretty clear that any "async function()" will return a promise, NOT a value. That's fine. But how do you get access to the value? If the only answer is "a callback", that's fine - but I was hoping

How to measure performance of awaiting asynchronous operations?

僤鯓⒐⒋嵵緔 提交于 2019-12-29 08:39:06
问题 I have a Windows Service that reads from multiple MessageQueue instances. Those messagequeues all run their own Task for reading messages. Normally, after reading a message, the work of an I/O database is done. I've found articles claiming it's a good idea to use async on I/O operations, because it would free up threads. I'm trying to simulate the performance boost of using async I/O opertations in a Console application. The Console application In my test environment, I have 10 queues.

Starting multiple async/await functions at once and handling them separately

…衆ロ難τιáo~ 提交于 2019-12-29 08:33:27
问题 How do you start multiple HttpClient.GetAsync() requests at once, and handle them each as soon as their respective responses come back? First what I tried is: var response1 = await client.GetAsync("http://example.com/"); var response2 = await client.GetAsync("http://stackoverflow.com/"); HandleExample(response1); HandleStackoverflow(response2); But of course it's still sequential. So then I tried starting them both at once: var task1 = client.GetAsync("http://example.com/"); var task2 =

Please explain Timer event async/await syntax

半城伤御伤魂 提交于 2019-12-29 07:54:30
问题 I researched the asynch and await syntax here and here. It really helps to understand the usage but I found an intriguing syntax example on MSDN which I just don't understand. Question : Could someone please explain to me the syntax of this System.Timers.Timer event registration with asynch await: Why can you use the async await keywords already in the lambda expression? Timer timer = new Timer(1000); timer.Elapsed += async ( sender, e ) => await HandleTimer(); private Task HandleTimer() {

Please explain Timer event async/await syntax

£可爱£侵袭症+ 提交于 2019-12-29 07:54:07
问题 I researched the asynch and await syntax here and here. It really helps to understand the usage but I found an intriguing syntax example on MSDN which I just don't understand. Question : Could someone please explain to me the syntax of this System.Timers.Timer event registration with asynch await: Why can you use the async await keywords already in the lambda expression? Timer timer = new Timer(1000); timer.Elapsed += async ( sender, e ) => await HandleTimer(); private Task HandleTimer() {

How to better understand the code/statements from “Async - Handling multiple Exceptions” article?

我只是一个虾纸丫 提交于 2019-12-29 07:17:09
问题 Running the following C# console app class Program { static void Main(string[] args) { Tst(); Console.ReadLine(); } async static Task Tst() { try { await Task.Factory.StartNew (() => { Task.Factory.StartNew (() => { throw new NullReferenceException(); } , TaskCreationOptions.AttachedToParent ); Task.Factory.StartNew ( () => { throw new ArgumentException(); } ,TaskCreationOptions.AttachedToParent ); } ); } catch (AggregateException ex) { // this catch will never be target Console.WriteLine("**