|
1 | 1 | namespace System.Threading.Tasks.Flow |
2 | 2 | { |
| 3 | + /// <summary> |
| 4 | + /// A task flow implementation that executes tasks on the thread that calls the <see cref="Run"/> method. |
| 5 | + /// </summary> |
| 6 | + /// <remarks> |
| 7 | + /// <para> |
| 8 | + /// The <see cref="CurrentThreadTaskFlow"/> class provides a task scheduling mechanism that |
| 9 | + /// executes tasks on a specific thread provided by the caller. Unlike <see cref="DedicatedThreadTaskFlow"/>, |
| 10 | + /// which creates its own thread, this class allows you to use an existing thread for task execution. |
| 11 | + /// </para> |
| 12 | + /// <para> |
| 13 | + /// This implementation is useful when you want to execute tasks on a specific thread, such as: |
| 14 | + /// </para> |
| 15 | + /// <list type="bullet"> |
| 16 | + /// <item>A UI thread in a desktop application</item> |
| 17 | + /// <item>A worker thread that you manage externally</item> |
| 18 | + /// <item>A thread with specific properties or priority settings</item> |
| 19 | + /// </list> |
| 20 | + /// <para> |
| 21 | + /// Note that the <see cref="Run"/> method must be called to start the task flow, and it |
| 22 | + /// will block the calling thread until the task flow is disposed. |
| 23 | + /// </para> |
| 24 | + /// <example> |
| 25 | + /// Basic usage with a background thread: |
| 26 | + /// <code> |
| 27 | + /// var taskFlow = new CurrentThreadTaskFlow(); |
| 28 | + /// |
| 29 | + /// // Start the task flow on a background thread |
| 30 | + /// var thread = new Thread(() => taskFlow.Run()) { IsBackground = true }; |
| 31 | + /// thread.Start(); |
| 32 | + /// |
| 33 | + /// // Enqueue tasks for execution on the background thread |
| 34 | + /// var task1 = taskFlow.Enqueue(() => Console.WriteLine("Task 1")); |
| 35 | + /// var task2 = taskFlow.Enqueue(() => Console.WriteLine("Task 2")); |
| 36 | + /// |
| 37 | + /// // Wait for tasks to complete and dispose the task flow |
| 38 | + /// await Task.WhenAll(task1, task2); |
| 39 | + /// await taskFlow.DisposeAsync(); |
| 40 | + /// </code> |
| 41 | + /// </example> |
| 42 | + /// </remarks> |
3 | 43 | public sealed class CurrentThreadTaskFlow : ThreadTaskFlow |
4 | 44 | { |
5 | 45 | private int _managedThreadId; |
6 | 46 |
|
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of the <see cref="CurrentThreadTaskFlow"/> class with default options. |
| 49 | + /// </summary> |
| 50 | + /// <remarks> |
| 51 | + /// This constructor uses the default options from <see cref="TaskFlowOptions.Default"/>. |
| 52 | + /// The task flow will not start processing tasks until the <see cref="Run"/> method is called. |
| 53 | + /// </remarks> |
7 | 54 | public CurrentThreadTaskFlow() |
8 | 55 | : this(TaskFlowOptions.Default) |
9 | 56 | { |
10 | 57 | } |
11 | 58 |
|
| 59 | + /// <summary> |
| 60 | + /// Initializes a new instance of the <see cref="CurrentThreadTaskFlow"/> class with the specified options. |
| 61 | + /// </summary> |
| 62 | + /// <param name="options">The options that configure the behavior of this task flow.</param> |
| 63 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> is null.</exception> |
| 64 | + /// <remarks> |
| 65 | + /// The task flow will not start processing tasks until the <see cref="Run"/> method is called. |
| 66 | + /// </remarks> |
12 | 67 | public CurrentThreadTaskFlow(TaskFlowOptions options) |
13 | 68 | : base(options) |
14 | 69 | { |
15 | 70 | } |
16 | 71 |
|
| 72 | + /// <summary> |
| 73 | + /// Gets the managed thread ID of the thread that is executing tasks in this task flow. |
| 74 | + /// </summary> |
| 75 | + /// <remarks> |
| 76 | + /// This property returns the thread ID of the thread that called the <see cref="Run"/> method. |
| 77 | + /// If the <see cref="Run"/> method has not been called yet, the thread ID is not defined. |
| 78 | + /// </remarks> |
17 | 79 | public override int ThreadId => _managedThreadId; |
18 | 80 |
|
| 81 | + /// <summary> |
| 82 | + /// Starts the task flow execution on the current thread and blocks until the task flow is disposed. |
| 83 | + /// </summary> |
| 84 | + /// <remarks> |
| 85 | + /// <para> |
| 86 | + /// This method must be called to start the task flow and begin processing tasks. |
| 87 | + /// It will block the calling thread until the task flow is disposed. |
| 88 | + /// </para> |
| 89 | + /// <para> |
| 90 | + /// Tasks enqueued before calling this method will be processed once it is called. |
| 91 | + /// Tasks enqueued after calling this method will be processed as they are received. |
| 92 | + /// </para> |
| 93 | + /// <para> |
| 94 | + /// To stop the execution, dispose the task flow from another thread or by using a cancellation token. |
| 95 | + /// </para> |
| 96 | + /// </remarks> |
19 | 97 | public void Run() |
20 | 98 | { |
21 | 99 | Starting(); |
|
0 commit comments