async-await

Observable from chained Tasks

空扰寡人 提交于 2019-12-18 04:24:14
问题 I'm trying to create an Observable where each item is produced via an asynchronous task. The next item should be produced via an async call on the result of the previous item (co-recursion). In "Generate" parlance this would look something like this - except that Generate does not support async (nor does it support the delegate on the initial state. var ob = Observable.Generate( async () => await ProduceFirst(), // Task<T> ProduceFirst() prev => Continue(prev) // bool Continue(T); async prev

MongoDB (server v 2.6.7) with C# driver 2.0: How to get the result from InsertOneAsync

 ̄綄美尐妖づ 提交于 2019-12-18 04:08:36
问题 I am testing MongoDB (server v 2.6.7) with the C# driver 2.0. When I am using the insert function InsertOneAsync for a document with an _id which exists I am expecting an error like the one you get from the Mongo shell: WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.Commands.$_id_ dup key: { : 0.0 }" }}) But the problem is that the insert with the C# driver does not throw an exception and

simply stop an async method

家住魔仙堡 提交于 2019-12-18 04:00:33
问题 I have this method which plays a sound, when the user taps on the screen, & I want it to stop playing it when the user taps the screen again. But the problem is "DoSomething()" method doesn't stop, it keeps going till it finishes. bool keepdoing = true; private async void ScreenTap(object sender, System.Windows.Input.GestureEventArgs e) { keepdoing = !keepdoing; if (!playing) { DoSomething(); } } private async void DoSomething() { playing = true; for (int i = 0; keepdoing ; count++) { await

Async all the way down?

泄露秘密 提交于 2019-12-18 03:56:13
问题 Trying to understand the new async/await pattern, I have one question which I can't find an answer to, namely if I should decorate my methods with async, if I intend to call those methods from other async functions, or just return Task s where appropriate? In other words, which of these classes A, B or C is best, and why? class A<T> { public async Task<T> foo1() //Should be consumed { return await foo2(); } public async Task<T> foo2() //Could be consumed { return await foo3(); } private async

How do I implement polling using Observables?

血红的双手。 提交于 2019-12-18 03:35:11
问题 I have a parametrized rest call that should be executed every five seconds with different params: Observable<TResult> restCall = api.method1(param1); I need to create an Observable<TResult> which will poll the restCall every 5 seconds with different values for param1. If the api call fails I need to get an error and make the next call in 5 seconds. The interval between calls should be measured only when restCall is finished (success/error). I'm currently using RxJava, but a .NET example would

Awaiting tasks: Return task or await if no code after await [duplicate]

纵然是瞬间 提交于 2019-12-18 03:12:47
问题 This question already has answers here : At the end of an async method, should I return or await? (2 answers) Closed 6 years ago . I've done a bit of reading, and think I have grasped the basics of the await and async keywords, with regards to System.Threading.Task. I'm not sure if I'm right over a small issue, however, and am looking for verification or for someone to correct me. I'm implementing an async method, with this signature: public Task ProcessUploadedFile(FileInfo info, string

Awaiting tasks: Return task or await if no code after await [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-18 03:12:03
问题 This question already has answers here : At the end of an async method, should I return or await? (2 answers) Closed 6 years ago . I've done a bit of reading, and think I have grasped the basics of the await and async keywords, with regards to System.Threading.Task. I'm not sure if I'm right over a small issue, however, and am looking for verification or for someone to correct me. I'm implementing an async method, with this signature: public Task ProcessUploadedFile(FileInfo info, string

Is it possible to await async tasks during a button click?

折月煮酒 提交于 2019-12-18 03:09:12
问题 I have a refresh button in my app that uses some async methods to update the list of items displayed. The problem is that I can't have a return type of Task for the event handler for the button click so I'm left with an async void method. Thus, the user can hit the refresh button, then select an item while the list is being repopulated which will result in an error. start of code that handles button click: private async void Button_Click_1(object sender, RoutedEventArgs e) { await ViewModel

What is the Operator Precedence of Await?

时光毁灭记忆、已成空白 提交于 2019-12-18 03:04:38
问题 In Javascript certain operators are processed before others: 1 + 2 * 3 // 1 + (2 * 3) // 7 because * has higher precedence than + 1 === 0 + 1 // 1 === (0 + 1) // true because + has a higher precedence than === The MDN has a full breakdown of all operators and their precedence ... except await . await getFoo() * 2; // await (getFoo() * 2) or (await getFoo()) * 2? await getFoo() === 5; // await (getFoo() === 5) or (await getFoo()) === 5? Can anyone explain which operators are processed before

Using async await when implementing a library with both synchronous and asynchronous API for the same functionality

冷暖自知 提交于 2019-12-18 02:46:45
问题 I've got a few questions about how to provide both synchronous and asynchronous implementation of the same functionality in a library. I am gonna ask them first and then provide the example code below (which is actually quite a bit, but in fact it's quite simple). Is there an approach to avoid violating the DRY principle? Consider the implementations of JsonStreamReader.Read , JsonStreamWriter.Write , JsonStreamWriter.Flush , ProtocolMessenger.Send , ProtocolMessenger.Receive and their