From c6d48e27a933e9b7608c8cb94ef61e52f76d2364 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Fri, 9 Apr 2021 16:01:29 +0300 Subject: [PATCH 1/4] Create Cancellation-Token-Provider.md --- docs/en/Cancellation-Token-Provider.md | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 docs/en/Cancellation-Token-Provider.md diff --git a/docs/en/Cancellation-Token-Provider.md b/docs/en/Cancellation-Token-Provider.md new file mode 100644 index 0000000000..1da3062de6 --- /dev/null +++ b/docs/en/Cancellation-Token-Provider.md @@ -0,0 +1,71 @@ +# Cancellation Token Provider + +A `CancellationToken` enables cooperative cancellation between threads, thread pool work items, or `Task` objects. To handle the possible cancellation of the operation, ABP Framework provides `ICancellationTokenProvider` to obtain the `CancellationToken` itself from the source. + +> To get more information about `CancellationToken`, see [Microsoft Documentation](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken). + +## ICancellationTokenProvider + +`ICancellationTokenProvider` is an abstraction to provide `CancellationToken` for different scenarios. + +Generally, you should pass the `CancellationToken` as a parameter for your method to use it. With the `ICancellationTokenProvider` you don't need to pass `CancellationToken` for every method. `ICancellationTokenProvider` can be injected with the **dependency injection** and provides the token source. + +**Example:** + +```csharp +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; + +namespace MyProject +{ + public class MyService : ITransientDependency + { + private readonly ICancellationTokenProvider _cancellationTokenProvider; + + public MyService(ICancellationTokenProvider cancellationTokenProvider) + { + _cancellationTokenProvider = cancellationTokenProvider; + } + + public async Task DoItAsync() + { + while (_cancellationTokenProvider.Token.IsCancellationRequested == false) + { + // Todo + } + } + } +} +``` + +## Built-in providers + +- `NullCancellationTokenProvider` + + The `NullCancellationTokenProvider` is a built in provider and it supply always `CancellationToken.None`. + +- `HttpContextCancellationTokenProvider` + + The `HttpContextCancellationTokenProvider` is a built in default provider for ABP Web applications. It simply provides a `CancellationToken` that is source of the web request from the `HttpContext`. + +## Implementing the ICancellationTokenProvider + +You can easily create your CancellationTokenProvider by creating a class that implements the `ICancellationTokenProvider` interface, as shown below: + +```csharp +using System.Threading; + +namespace AbpDemo +{ + public class MyCancellationTokenProvider : ICancellationTokenProvider + { + public CancellationToken Token { get; } + + private MyCancellationTokenProvider() + { + + } + } +} +``` From a22f172d4e0b065e171d4cc1bb0bcb6a59f188d1 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Fri, 9 Apr 2021 16:04:44 +0300 Subject: [PATCH 2/4] Update docs-nav.json --- docs/en/docs-nav.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index 5e0f0b469a..3bb926190e 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -176,6 +176,10 @@ { "text": "Logging", "path": "Logging.md" + }, + { + "text": "Cancellation Token Provider", + "path": "Cancellation-Token-Provider.md" } ] }, From 4dc13ed0ec5afa972555d418e6c43ee6f4a44eb9 Mon Sep 17 00:00:00 2001 From: Ahmet Date: Fri, 9 Apr 2021 16:06:39 +0300 Subject: [PATCH 3/4] Update Cancellation-Token-Provider.md --- docs/en/Cancellation-Token-Provider.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/Cancellation-Token-Provider.md b/docs/en/Cancellation-Token-Provider.md index 1da3062de6..7f7a78f8ad 100644 --- a/docs/en/Cancellation-Token-Provider.md +++ b/docs/en/Cancellation-Token-Provider.md @@ -8,7 +8,7 @@ A `CancellationToken` enables cooperative cancellation between threads, thread p `ICancellationTokenProvider` is an abstraction to provide `CancellationToken` for different scenarios. -Generally, you should pass the `CancellationToken` as a parameter for your method to use it. With the `ICancellationTokenProvider` you don't need to pass `CancellationToken` for every method. `ICancellationTokenProvider` can be injected with the **dependency injection** and provides the token source. +Generally, you should pass the `CancellationToken` as a parameter for your method to use it. With the `ICancellationTokenProvider` you don't need to pass `CancellationToken` for every method. `ICancellationTokenProvider` can be injected with the **dependency injection** and provides the token from it's source. **Example:** @@ -32,7 +32,7 @@ namespace MyProject { while (_cancellationTokenProvider.Token.IsCancellationRequested == false) { - // Todo + // ... } } } From 2846dab050a7fcbc42447a1a4efb6f5addae85dc Mon Sep 17 00:00:00 2001 From: Ahmet Date: Fri, 9 Apr 2021 16:36:12 +0300 Subject: [PATCH 4/4] Update docs-nav.json --- docs/en/docs-nav.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/docs-nav.json b/docs/en/docs-nav.json index 3bb926190e..4d2b2419c2 100644 --- a/docs/en/docs-nav.json +++ b/docs/en/docs-nav.json @@ -176,10 +176,6 @@ { "text": "Logging", "path": "Logging.md" - }, - { - "text": "Cancellation Token Provider", - "path": "Cancellation-Token-Provider.md" } ] }, @@ -347,6 +343,10 @@ { "text": "Virtual File System", "path": "Virtual-File-System.md" + }, + { + "text": "Cancellation Token Provider", + "path": "Cancellation-Token-Provider.md" } ] },