In .NET, a Task
represents an asynchronous operation and is a core component of the Task Parallel Library (TPL). It provides a way to run and manage asynchronous code, making it easier to write, manage, and understand asynchronous programming.
از تسک زمانی استفاده میشه که بخوایم یه کاری رو انجام بدیم ولی thread اصلی رو نگه نداریم و کارها به صورت موازی انجام بشن و کد ها به صورت async هندل بشن
Key Features of Task
:
-
Asynchronous Execution:
Task
allows you to run code asynchronously, meaning the operation can execute without blocking the main thread.
-
Return Values:
- A
Task
can return a value. This is done usingTask<T>
, whereT
is the type of the result. If the task doesn’t return a value, it’s simply aTask
.
- A
خوب task T اون T یعنی تایپ رون چیزی که میخواد Return کنه
-
Composability:
- Tasks can be composed using methods like
ContinueWith
,WhenAll
,WhenAny
, andAwait
, enabling complex chains of asynchronous operations. با یه سری متد که نوشته میتونیم یه زنجیره ای از کار ها رو به صورت async هندل کنیم
- Tasks can be composed using methods like
-
Exception Handling:
- Tasks provide a robust mechanism for handling exceptions that occur during asynchronous operations. Exceptions can be caught and handled when the task is awaited or by checking the
Task.Exception
property.
- Tasks provide a robust mechanism for handling exceptions that occur during asynchronous operations. Exceptions can be caught and handled when the task is awaited or by checking the
-
Cancellation Support:
- You can cancel a task by using a
CancellationToken
. This provides a way to terminate a task before it completes if necessary.
- You can cancel a task by using a
-
Concurrency:
- Tasks make it easy to write code that can run concurrently, leveraging multiple CPU cores when available, improving performance.
Types of Tasks:
Task
: Represents an asynchronous operation that does not return a value.Task<T>
: Represents an asynchronous operation that returns a value of typeT
.
Use Cases:
- Asynchronous I/O Operations: Reading from a file, making web requests.
- Background Processing: Running tasks that should not block the main thread, such as processing data or performing calculations.
- Concurrency: Running multiple operations in parallel.
Conclusion:
In .NET, Task
is a fundamental concept for implementing asynchronous programming, helping to improve application performance, responsiveness, and scalability.