Skip to content

Commit

Permalink
docs: update the changes of Alis.Core.Aspect.Thread module
Browse files Browse the repository at this point in the history
  • Loading branch information
pabllopf committed Jan 30, 2024
1 parent 466bab5 commit 869795f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
38 changes: 25 additions & 13 deletions 6_Ideation/Thread/src/ThreadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Alis.Core.Aspect.Thread
{
Expand All @@ -40,40 +41,51 @@ public class ThreadManager
/// <summary>
/// The cancellation token source
/// </summary>
private List<(System.Threading.Thread, CancellationTokenSource)> threads = new List<(System.Threading.Thread, CancellationTokenSource)>();

private Dictionary<ThreadTask, CancellationTokenSource> threadTokens = new Dictionary<ThreadTask, CancellationTokenSource>();
/// <summary>
/// Starts the thread using the specified task
/// Starts the thread using the specified thread task
/// </summary>
/// <param name="task">The task</param>
public void StartThread(ThreadTask task)
/// <param name="threadTask">The thread task</param>
public void StartThread(ThreadTask threadTask)
{
CancellationTokenSource cts = new CancellationTokenSource();
System.Threading.Thread thread = new System.Threading.Thread(() => task.Execute());
threads.Add((thread, cts));
thread.Start();
threadTokens.Add(threadTask, cts);
Task.Run(() => threadTask.Execute(cts.Token), cts.Token);
}

/// <summary>
/// Stops the thread using the specified thread task
/// </summary>
/// <param name="threadTask">The thread task</param>
public void StopThread(ThreadTask threadTask)
{
if (threadTokens.TryGetValue(threadTask, out var cts))
{
cts.Cancel();
threadTokens.Remove(threadTask);
}
}

/// <summary>
/// Stops the all threads
/// </summary>
public void StopAllThreads()
{
foreach ((System.Threading.Thread thread, CancellationTokenSource cts) in threads)
foreach (CancellationTokenSource cts in threadTokens.Values)
{
cts.Cancel();
}

threads.Clear();
threadTokens.Clear();
}

/// <summary>
/// Gets the thread count
/// </summary>
/// <returns>The int</returns>
public int GetThreadCount()
{
return threads.Count;
return threadTokens.Count;
}
}
}
19 changes: 12 additions & 7 deletions 6_Ideation/Thread/src/ThreadManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ The `ThreadManager` class is part of the `Alis.Core.Aspect.Thread` namespace. It

## Properties

- `List<System.Threading.Thread> threads`: This private property holds a list of threads that are managed by the `ThreadManager`.
- `Dictionary<ThreadTask, CancellationTokenSource> threadTokens`: This private property holds a dictionary of threads and their cancellation tokens that are managed by the `ThreadManager`.

## Methods

- `void StartThread(ThreadTask task)`: This method starts a new thread with the given task. It creates a new `System.Threading.Thread` instance, adds it to the `threads` list, and starts the thread.
- `void StartThread(ThreadTask threadTask)`: This method starts a new thread with the given task. It creates a new `CancellationTokenSource`, adds it to the `threadTokens` dictionary, and starts the task in a new thread.

- `void StopAllThreads()`: This method stops all threads that are currently alive and clears the `threads` list.
- `void StopThread(ThreadTask threadTask)`: This method stops a specific thread. It retrieves the `CancellationTokenSource` for the given task from the `threadTokens` dictionary, cancels the token, and removes the task from the dictionary.

- `void StopAllThreads()`: This method stops all threads that are currently alive and clears the `threadTokens` dictionary.

- `int GetThreadCount()`: This method returns the current number of active threads managed by the `ThreadManager`.

## Usage

Expand All @@ -19,7 +23,7 @@ Here is an example of how to use the `ThreadManager` class:
```csharp
ThreadManager manager = new ThreadManager();

ThreadTask task = new ThreadTask(() =>
ThreadTask task = new ThreadTask(token =>
{
// Your code here
});
Expand All @@ -28,7 +32,8 @@ manager.StartThread(task);

// ...
manager.StopAllThreads();
```
manager.StopThread(task);

// ...
In this example, a new `ThreadManager` is created. A `ThreadTask` is then created and started in a new thread by the `ThreadManager`. Later, all threads are stopped by calling the `StopAllThreads` method.
manager.StopAllThreads();
2 changes: 1 addition & 1 deletion 6_Ideation/Thread/src/ThreadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ThreadTask(Action<CancellationToken> action, CancellationToken token)
/// <summary>
/// Executes this instance
/// </summary>
public void Execute()
public void Execute(CancellationToken token)
{
Action(Token);
}
Expand Down
19 changes: 9 additions & 10 deletions 6_Ideation/Thread/src/ThreadTask.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
# ThreadTask Class

The `ThreadTask` class is part of the `Alis.Core.Aspect.Thread` namespace. It is used to encapsulate an action that can be executed in a separate thread.
The `ThreadTask` class is part of the `Alis.Core.Aspect.Thread` namespace. It is used to encapsulate a task that will be executed in a separate thread.

## Properties

- `Action Action { get; set; }`: This property holds the action that will be executed when the `Execute` method is called.
- `Action<CancellationToken> Action`: This private property holds the action that will be executed when the task is started.
- `CancellationToken Token`: This private property holds the cancellation token that can be used to request the cancellation of the task.

## Constructor

- `ThreadTask(Action action)`: This constructor initializes a new instance of the `ThreadTask` class. It takes an `Action` as a parameter, which is the action that will be executed when the `Execute` method is called.
- `ThreadTask(Action<CancellationToken> action, CancellationToken token)`: This constructor creates a new instance of the `ThreadTask` class with the given action and cancellation token.

## Methods

- `void Execute()`: This method invokes the action that was passed to the constructor. It does not return a value.
- `void Execute(CancellationToken token)`: This method executes the action of the task, passing the cancellation token to it.

## Usage

Here is an example of how to use the `ThreadTask` class:

```csharp
ThreadTask task = new ThreadTask(() =>
CancellationTokenSource cts = new CancellationTokenSource();
ThreadTask task = new ThreadTask(token =>
{
// Your code here
});
}, cts.Token);

task.Execute();
```

In this example, a new `ThreadTask` is created with an action. The action is then executed by calling the `Execute` method.
task.Execute(cts.Token);
2 changes: 1 addition & 1 deletion 6_Ideation/Thread/test/ThreadTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Execute_ShouldExecuteAction()
}, cts.Token);

// Act
threadTask.Execute();
threadTask.Execute(cts.Token);

//wait 1s
System.Threading.Thread.Sleep(1000);
Expand Down

0 comments on commit 869795f

Please sign in to comment.