async-await

TestMethod: async Task TestSth() does not work with .NET 4.0

非 Y 不嫁゛ 提交于 2019-12-21 04:25:29
问题 I'm trying to run asynchronous test methods with .NET 4.0 BCL Async and MsTest. It seems that this setup is not able to deal with [TestMethod] async Task TestSth() due to a missing entry in the test case explorer. After changing the signature to async void , I can run the the test case but with the wrong outcome (no errors will be reported at all). I have seen an attemt at Running Async Task unit tests with TFS 2010 but I think there should be a prettier way to tackle the problem. Any

Does Task.Delay start a new thread?

荒凉一梦 提交于 2019-12-21 04:03:01
问题 The following code should (at least in my opinion) create 100 Tasks , which are all waiting in parallel (that's the point about concurrency, right :D ?) and finish almost at the same time. I guess for every Task.Delay a Timer object is created internally. public static async Task MainAsync() { var tasks = new List<Task>(); for (var i = 0; i < 100; i++) { Func<Task> func = async () => { await Task.Delay(1000); Console.WriteLine("Instant"); }; tasks.Add(func()); } await Task.WhenAll(tasks); }

Locking with nested async calls

被刻印的时光 ゝ 提交于 2019-12-21 04:02:43
问题 I am working on a multi threaded WindowsPhone8 app that has critical sections within async methods. Does anyone know of a way to properly use semaphores / mutexes in C# where you are using nested async calls where the inner method may be acquiring the same lock that it already acquired up the callstack? I thought the SemaphoreSlim might be the answer, but it looks like it causes a deadlock. public class Foo { SemaphoreSlim _lock = new SemaphoreSlim(1); public async Task Bar() { await _lock

The awaitable and awaiter In C# 5.0 Asynchronous

爱⌒轻易说出口 提交于 2019-12-21 04:02:35
问题 Task or Task<TResult> object is awaitable, so we can use await key on those whose return value is Task or Task<TResult>. Task or Task<TResult> are the most frequently-used awaitable object. We also can define our own awaitable object.The object should has below qualification. It has a GetAwaiter() method (instance method or extension method); Its GetAwaiter() method returns an awaiter. An object is an awaiter if: It implements INotifyCompletion or ICriticalNotifyCompletion interface; It has

Semaphore thread throttling with async/await

大城市里の小女人 提交于 2019-12-21 03:54:20
问题 I recently came across an example of throttling threads for async/await calls. After analyzing and playing with the code on my machine, I came up with a slightly different way of doing the same thing. What I'm uncertain about is wether what is happening under the hood is pretty much the same or if there are any subtle differences worth noting? Here's the code based on the original example: private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(5); public async Task CallThrottledTasks()

Do all C++ compilers support the async/await keywords?

女生的网名这么多〃 提交于 2019-12-21 03:47:09
问题 I want to use async/await syntax in C++ (UE4 framework), but due to cross-platform code I not sure that is possible... Or possible? If yes, how can I use it? And also there are await and __await ( resumable , yield and __yield_value also) keywords that highlighted in Visual Studio. What's difference? Maybe not all compilers supports this keywords or supports separately? gcc , clang are accepts it? Or not accepts and I can just use macros for each platform individually. 回答1: async and await

How do I await a response from an RX Subject without introducing a race condition?

☆樱花仙子☆ 提交于 2019-12-21 03:42:56
问题 I have a service that allows a caller to send commands and receive responses asynchronously. In a real application, these actions are fairly disconnected (some action will send a command, and the responses will be process independently). However, in my tests, I need to be able to send a command and then wait for the (first) response before continuing the test. The responses are published using RX, and my first attempt at the code was something like this: service.SendCommand("BLAH"); await

Why do async unit tests fail when the async/await keywords aren't used?

本秂侑毒 提交于 2019-12-21 03:34:15
问题 According to this discussion, there should be no difference between the following two methods: public async Task Foo() { await DoSomethingAsync(); } public Task Foo() { return DoSomethingAsync(); } Actually, it would seem that for very simple methods, the invocation without the async/await keywords would be preferred, as they remove some overhead. However, this apparently doesn't always work in unit tests. MSTest [TestClass] public class AsyncTest { [TestMethod] public async Task Test1() {

Asynchronous map function that await's returns Promise instead of value

孤街醉人 提交于 2019-12-20 18:30:36
问题 I have this code async function addFiles(dir,tree) { return (await readDir(dir)) .map(async (name) => {await readDir(dir); return name;}) } but unfortunately, it just returns a bunch of promises, because there the async function in map is not waited upon. I'm wondering if there is any way to await the mapped function in the above code. 回答1: try async function addFiles(dir,tree) { const files = await readDir(dir) await Promise.all(files.map(async (name) => {await readDir(dir); return name;}) }

Await Tasks in Test Setup Code in xUnit.net?

梦想与她 提交于 2019-12-20 16:52:50
问题 The exact situation is I'm doing E2E tests with Protractor.NET (.NET port of AngularJS's Protractor E2E framework) and I would like to make some web requests (and the API -- System.Net.Http.HttpClient -- has all Async/ Task methods) to Arrange my test before I Act/Assert, only I need to do this same Arrange-ing for several tests. I'm using xUnit.net as my test runner they use an interface ( IUseFixture<T> ) for per-fixture setup code. It would be nice if there was a IAsyncUseFixture<T> that