Task task = Task.Run(() => { // Some code here... }); // Wait for the task to complete task.Wait(); // Dispose the task task.Dispose();
using System.Threading.Tasks; public async Task MyAsyncMethod() { // Some code here... // Wait for another async task to complete Task subTask = DoSomethingAsync(); await subTask; // Dispose the subtask subTask.Dispose(); }In this example, we have an async method that waits for a subtask to complete. After the subtask completes, we dispose of it using the Dispose() method. The System.Threading.Tasks namespace contains the Task class and other related types that enable you to implement a task-based asynchronous pattern.