For example I have some synchronous functions that I want to run in another thread. I found the way with closure like this. Is there any more efficient and beautiful way if i don't want to create any new functions? Am I reinventing the wheel or is it alright?
class Program
{
static void Main(string[] args)
{
((Action)(async () =>
{
await Task.Run(() => LongOperation());
Console.WriteLine("Then callback triggered");
}))();
Console.WriteLine("Main thread finished first.");
Console.ReadKey();
}
static void LongOperation()
{
Thread.Sleep(3000);
Console.WriteLine("Work completed");
}
}
If I want just to perform the function asynchronously I can just type
Task.Run(() => LongOperation());
But what if I want to add some lines of code for callback like in JavaScript?