Task
in C# is used when performing operations in an asynchronous manner on the thread pool rather than on the main thread used by the application.
Task
is part of the System.Threading.Tasks
namespace.
Creating a Task
In order to create a task we do the following:
Task task1 = new Task(nameOfMethod);
Starting the Task
task1.start();
Task.Run()
We can use Task.Run()
in order to queue an operation to be carried out on a thread pool. Note if this is used more than once, then the operations will run parallel to each other.
Wait()
We use Wait()
after getting the task to run in order to wait until the task is complete. Using the above example, we would use it in the following manner: task1.Wait();