async-await

Queue of async tasks with throttling which supports muti-threading

谁都会走 提交于 2020-01-10 07:31:21
问题 I need to implement a library to request vk.com API. The problem is that API supports only 3 requests per second. I would like to have API asynchronous. Important: API should support safe accessing from multiple threads. My idea is implement some class called throttler which allow no more than 3 request/second and delay other request. The interface is next: public interface IThrottler : IDisposable { Task<TResult> Throttle<TResult>(Func<Task<TResult>> task); } The usage is like var audio =

Queue of async tasks with throttling which supports muti-threading

无人久伴 提交于 2020-01-10 07:31:09
问题 I need to implement a library to request vk.com API. The problem is that API supports only 3 requests per second. I would like to have API asynchronous. Important: API should support safe accessing from multiple threads. My idea is implement some class called throttler which allow no more than 3 request/second and delay other request. The interface is next: public interface IThrottler : IDisposable { Task<TResult> Throttle<TResult>(Func<Task<TResult>> task); } The usage is like var audio =

UWP app: FileOpenPicker PickSingleFileAsync() can't await

淺唱寂寞╮ 提交于 2020-01-10 04:58:07
问题 I'm trying to let the user browse for a file with the FileOpenPicker class, but when I use the PickSingleFileAsync function with await, I get following error: 'IAsyncOperation' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation' could be found (are you missing a using directive for 'System'?) This is the function that gives the error on it's last line: private async void browseFileButton_Click(object sender,

Passing async method into Parallel.ForEach

若如初见. 提交于 2020-01-10 04:37:08
问题 I was reading this post about Parallel.ForEach where it was stated that "Parallel.ForEach is not compatible with passing in a async method." So, to check I write this code: static async Task Main(string[] args) { var results = new ConcurrentDictionary<string, int>(); Parallel.ForEach(Enumerable.Range(0, 100), async index => { var res = await DoAsyncJob(index); results.TryAdd(index.ToString(), res); }); Console.ReadLine(); } static async Task<int> DoAsyncJob(int i) { Thread.Sleep(100); return

How to call one of multiple functions - JavaScript?

梦想与她 提交于 2020-01-10 04:34:10
问题 I have three functions: Change Email Change password Change otherData And One Button to call them , when the user changes his data without change Email Or Password I don't wont to call other function Change Email or Change Password just call the function Change other data, and when changing his email with other data like username, location I just want to call Change Email, change other data Function NOT Change Password So how to handle this, and how to get a current password and save them in

Correctly cancel async operation and fire it again

北战南征 提交于 2020-01-10 03:15:09
问题 How to handle case, where user might hit the button, which invokes long running async operation, multiple time. My idea was first check if the async operation is running, cancel it and fire it again. So far I have tried to build this kind of functionality using CancellationTokenSource, but it is not working as expected. Some times there is two async operations running, so the "old" async oprations is not cancelled yet when I start new one and this mixes up the resul handling. Any suggestions

How does running several tasks asynchronously on UI thread using async/await work?

自古美人都是妖i 提交于 2020-01-09 19:32:28
问题 I've read (and used) async/await quite a lot for some time now but I still have one question I can't get an answer to. Say I have this code. private async void workAsyncBtn_Click(object sender, EventArgs e) { var myTask = _asyncAwaitExcamples.DoHeavyWorkAsync(5); await myTask; statusTextBox.Text += "\r\n DoHeavyWorkAsync message"; } It's called from the UI thread and returned to the UI Thread. Therefor I am able to do UI-specific things in this method and after the await myTask . If I had

How to call a generic async method using reflection

非 Y 不嫁゛ 提交于 2020-01-09 19:02:25
问题 public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task<T> Get<T>(T o) where T : IBar; } public class Foo : IFoo { public async Task<T> Get<T>(T o) where T : IBar { ... } } I can then call this method using reflection: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); var generic = method.MakeGenericMethod(bar2.GetType()); var task = generic.Invoke(foo, new [] { bar2 }); How do I await on this Task ? and How do I cast it to Task

How to call a generic async method using reflection

随声附和 提交于 2020-01-09 19:01:28
问题 public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task<T> Get<T>(T o) where T : IBar; } public class Foo : IFoo { public async Task<T> Get<T>(T o) where T : IBar { ... } } I can then call this method using reflection: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); var generic = method.MakeGenericMethod(bar2.GetType()); var task = generic.Invoke(foo, new [] { bar2 }); How do I await on this Task ? and How do I cast it to Task

How to call a generic async method using reflection

空扰寡人 提交于 2020-01-09 19:01:24
问题 public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task<T> Get<T>(T o) where T : IBar; } public class Foo : IFoo { public async Task<T> Get<T>(T o) where T : IBar { ... } } I can then call this method using reflection: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); var generic = method.MakeGenericMethod(bar2.GetType()); var task = generic.Invoke(foo, new [] { bar2 }); How do I await on this Task ? and How do I cast it to Task