S&box Wiki

Revision Difference

Threaded_Tasks#551508

<cat>Code.CSharp</cat>⤶ <cat>Code.Misc</cat>⤶ <title>Threaded Tasks</title> # About Sometimes you want to execute some CPU-heavy code and don't want to block or freeze up the main thread. You can offload these tasks onto a thread from the thread pool. # Usage ```csharp private async void MyMethod() { // This is executed on the main thread. // The following will execute a task on the thread pool: await GameTask.RunInThreadAsync( MyThreadedTask ); // The task has been completed off the main thread and we'll now continue executing on the main thread. Log.Info( "Done!" ); } private async Task MyThreadedTask() { await GameTask.Delay( 100 ); // Do something CPU-heavy. } ```