c#-5.0

Is there any async equivalent of Process.Start?

落爺英雄遲暮 提交于 2019-11-26 02:17:42
问题 Like the title suggests, is there an equivalent to Process.Start (allows you run another application or batch file) that I can await? I\'m playing with a small console app and this seemed like the perfect place to be using async and await but I can\'t find any documentation for this scenario. What I\'m thinking is something along these lines: void async RunCommand() { var result = await Process.RunAsync(\"command to run\"); } 回答1: Process.Start() only starts the process, it doesn't wait until

async at console app in C#? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-26 00:58:16
问题 This question already has an answer here: Can't specify the 'async' modifier on the 'Main' method of a console app 15 answers I have this simple code : public static async Task<int> SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await firstTask + await secondTask; } private async Task<int> GetOperationOneAsync() { await Task.Delay(500); // Just to simulate an operation taking time return 10; } private async Task<int>

Using async/await for multiple tasks

久未见 提交于 2019-11-26 00:45:16
问题 I\'m using an API client that is completely asynchrounous, that is, each operation either returns Task or Task<T> , e.g: static async Task DoSomething(int siteId, int postId, IBlogClient client) { await client.DeletePost(siteId, postId); // call API client Console.WriteLine(\"Deleted post {0}.\", siteId); } Using the C# 5 async/await operators, what is the correct/most efficient way to start multiple tasks and wait for them all to complete: int[] ids = new[] { 1, 2, 3, 4, 5 }; Parallel

An async/await example that causes a deadlock

三世轮回 提交于 2019-11-25 22:20:51
问题 I came across some best practices for asynchronous programming using c#\'s async / await keywords (I\'m new to c# 5.0). One of the advices given was the following: Stability: Know your synchronization contexts ... Some synchronization contexts are non-reentrant and single-threaded. This means only one unit of work can be executed in the context at a given time. An example of this is the Windows UI thread or the ASP.NET request context. In these single-threaded synchronization contexts, it’s

How would I run an async Task<T> method synchronously?

天涯浪子 提交于 2019-11-25 21:49:05
问题 I\'m learning about async/await, and ran into a situation where I need to call an async method synchronously. How can I do that? Async method: public async Task<Customers> GetCustomers() { return await Service.GetCustomersAsync(); } Normal usage: public async void GetCustomers() { customerList = await GetCustomers(); } I\'ve tried using the following: Task<Customer> task = GetCustomers(); task.Wait() Task<Customer> task = GetCustomers(); task.RunSynchronously(); Task<Customer> task =

Fire-and-forget with async vs “old async delegate”

大憨熊 提交于 2019-11-25 21:47:18
I am trying to replace my old fire-and-forget calls with a new syntax, hoping for more simplicity and it seems to be eluding me. Here's an example class Program { static void DoIt(string entry) { Console.WriteLine("Message: " + entry); } static async void DoIt2(string entry) { await Task.Yield(); Console.WriteLine("Message2: " + entry); } static void Main(string[] args) { // old way Action<string> async = DoIt; async.BeginInvoke("Test", ar => { async.EndInvoke(ar); ar.AsyncWaitHandle.Close(); }, null); Console.WriteLine("old-way main thread invoker finished"); // new way DoIt2("Test2");

async at console app in C#? [duplicate]

雨燕双飞 提交于 2019-11-25 20:50:44
This question already has an answer here: Can't specify the 'async' modifier on the 'Main' method of a console app 15 answers I have this simple code : public static async Task<int> SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await firstTask + await secondTask; } private async Task<int> GetOperationOneAsync() { await Task.Delay(500); // Just to simulate an operation taking time return 10; } private async Task<int> GetOperationTwoAsync() { await Task.Delay(100); // Just to simulate an operation taking time return 5; } Great.