Skip to content

Instantly share code, notes, and snippets.

@ezaurum
Created February 16, 2015 05:15
Show Gist options
  • Save ezaurum/746ea24220b52b27b56c to your computer and use it in GitHub Desktop.
Save ezaurum/746ea24220b52b27b56c to your computer and use it in GitHub Desktop.
await/async run test
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
internal class Program
{
private static ConcurrentQueue<int> _a = new ConcurrentQueue<int>();
private static void Main(string[] args)
{
Task.WaitAll(GetValue());
Console.ReadLine();
}
private static async Task GetValue()
{
var stringGetAsync = Test1Async();
var keyDeleteAsync = Test2Async();
if (!await ExecuteAsync()) return;
var target = await stringGetAsync;
var result = await keyDeleteAsync;
Console.WriteLine(target);
Console.WriteLine(result);
}
private static async Task<bool> ExecuteAsync()
{
Console.WriteLine("Execute runs." + _a.Count);
await Task.Delay(1000);
Console.WriteLine("Execute exit.");
return true;
}
private static async Task<bool> Test2Async()
{
Console.WriteLine("Test2Async runs." + _a.Count);
_a.Enqueue(122);
await Task.Delay(10);
Console.WriteLine("Test2 exit.");
return true;
}
private static async Task<string> Test1Async()
{
Console.WriteLine("Test1Async runs. " + _a.Count);
_a.Enqueue(12);
await Task.Delay(100);
Console.WriteLine("Test1 exit.");
return "OK";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment