Skip to content

Instantly share code, notes, and snippets.

@kostapc
Created January 10, 2020 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostapc/f7c8f54c655b901f5b07febc53f35bc3 to your computer and use it in GitHub Desktop.
Save kostapc/f7c8f54c655b901f5b07febc53f35bc3 to your computer and use it in GitHub Desktop.
sample of c# async usage
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