internal static async Task GenerateChunkAsync(Dictionary<Vector3Int, Chunk> chunks)
{
// Any main-thread setup here
var chunkData = await Task.Run(() =>
{
// Execute code on a background thread
var data = SomeFunc(5);
return data;
});
// Finish running on the main thread (important for opengl)
// For some reason, this always runs on the background thread that the Task.Run code was running on
Console.WriteLine($"Finished function in {chunkData}ms. Is main thread: {Engine.IsMainThread}");
}
private static long SomeFunc(int sleepTime)
{
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
// Simulate some background work
Thread.Sleep(sleepTime * 1000);
timer.Stop();
return timer.ElapsedMilliseconds;
}
internal static async Task GenerateChunkAsync(Dictionary<Vector3Int, Chunk> chunks)
{
// Any main-thread setup here
var chunkData = await Task.Run(() =>
{
// Execute code on a background thread
var data = SomeFunc(5);
return data;
});
// Finish running on the main thread (important for opengl)
// For some reason, this always runs on the background thread that the Task.Run code was running on
Console.WriteLine($"Finished function in {chunkData}ms. Is main thread: {Engine.IsMainThread}");
}
private static long SomeFunc(int sleepTime)
{
var timer = new System.Diagnostics.Stopwatch();
timer.Start();
// Simulate some background work
Thread.Sleep(sleepTime * 1000);
timer.Stop();
return timer.ElapsedMilliseconds;
}