Created
January 10, 2020 11:24
-
-
Save kostapc/f7c8f54c655b901f5b07febc53f35bc3 to your computer and use it in GitHub Desktop.
sample of c# async usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace FunctionalExperiments | |
{ | |
class AboutAsyncMain | |
{ | |
static void Main(string[] args) | |
{ | |
var runner = new AboutAsync(); | |
long time = Systime(); | |
int count = 3; | |
var tasks = new List<Task>(count); | |
for (int i = 0; i < count; i++) | |
{ | |
Task t = runner.AsyncCall(BlockingFunction,$"val={i}"); | |
tasks.Add(t); | |
} | |
tasks.ForEach((t) => | |
{ | |
t.Wait(); | |
}); | |
time = Systime() - time; | |
Console.WriteLine($"time is: {time}"); | |
Console.ReadKey(); | |
} | |
static void BlockingFunction(String val) | |
{ | |
Thread.Sleep(1000); | |
Console.WriteLine($"executed {val}"); | |
} | |
static long Systime() | |
{ | |
return DateTimeOffset.Now.ToUnixTimeMilliseconds(); | |
} | |
} | |
class AboutAsync | |
{ | |
public delegate void BFCall(String val); | |
public async Task AsyncCall(BFCall call, String val) | |
{ | |
await Task.Run(()=> | |
{ | |
call(val); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment