Skip to content

Commit 5ed27fe

Browse files
committed
Added multiple dotnet support.
1 parent c5d54a9 commit 5ed27fe

7 files changed

Lines changed: 154 additions & 131 deletions

File tree

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-

1+

22
using TaskMonitoring.Application.Models;
33

4-
namespace TaskMonitoring.Application;
5-
6-
public interface ITaskManager
4+
namespace TaskMonitoring.Application
75
{
8-
TaskInfo TrackTask(Task task);
9-
int GetPendingTaskCount();
10-
List<TaskInfo> GetTaskDetails();
6+
public interface ITaskManager
7+
{
8+
TaskInfo TrackTask(Task task);
9+
int GetPendingTaskCount();
10+
List<TaskInfo> GetTaskDetails();
11+
}
1112
}
13+
14+

src/TaskMonitoring.Application/IThreadPoolMonitor.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
using TaskMonitoring.Application.Models;
44

5-
namespace TaskMonitoring.Application;
6-
7-
public interface IThreadPoolMonitor
5+
namespace TaskMonitoring.Application
86
{
9-
int GetActiveThreadCount();
10-
ThreadPoolStatus GetThreadPoolStatus();
11-
void SetMinThreads(ThreadPoolStatus threadPoolStatus);
12-
void SetMaxThreads(ThreadPoolStatus threadPoolStatus);
7+
public interface IThreadPoolMonitor
8+
{
9+
int GetActiveThreadCount();
10+
ThreadPoolStatus GetThreadPoolStatus();
11+
void SetMinThreads(ThreadPoolStatus threadPoolStatus);
12+
void SetMaxThreads(ThreadPoolStatus threadPoolStatus);
13+
14+
}
15+
}
1316

14-
}
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-

1+
using System;
2+
using System.Threading.Tasks;
23
using TaskMonitoring.Application;
34

4-
Console.WriteLine("Hello, World!");
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("Hello, World!");
510

6-
var _taskManager = new TaskManager();
7-
var task = Task.Run(() => { });
11+
var _taskManager = new TaskManager();
12+
var task = Task.Run(() => { });
813

9-
var taskInfo = _taskManager.TrackTask(task);
10-
task.Wait();
14+
var taskInfo = _taskManager.TrackTask(task);
15+
task.Wait();
1116

12-
Console.WriteLine($"TaskId: {taskInfo.TaskId}");
13-
Console.WriteLine($"StartTime: {taskInfo.StartTime}");
14-
Console.WriteLine($"EndTime: {taskInfo.EndTime}");
15-
Console.WriteLine($"Status: {taskInfo.Status}");
16-
Console.WriteLine($"Exception: {taskInfo.Exception}");
17+
Console.WriteLine($"TaskId: {taskInfo.TaskId}");
18+
Console.WriteLine($"StartTime: {taskInfo.StartTime}");
19+
Console.WriteLine($"EndTime: {taskInfo.EndTime}");
20+
Console.WriteLine($"Status: {taskInfo.Status}");
21+
Console.WriteLine($"Exception: {taskInfo.Exception}");
1722

18-
Console.ReadLine();
23+
Console.ReadLine();
24+
}
25+
}

src/TaskMonitoring.Application/TaskManager.cs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,59 @@
22
using System.Collections.Concurrent;
33
using TaskMonitoring.Application.Models;
44

5-
namespace TaskMonitoring.Application;
6-
7-
public class TaskManager : ITaskManager
5+
namespace TaskMonitoring.Application
86
{
9-
10-
private readonly ConcurrentDictionary<int, TaskInfo> _tasks = new();
11-
12-
/// <summary>
13-
/// Retrieves the count of pending tasks, which are either waiting to run or currently running.
14-
/// </summary>
15-
/// <returns>The count of pending tasks.</returns>
16-
public int GetPendingTaskCount()
17-
{
18-
return _tasks.Values.Count(t => t.Status == TaskStatus.WaitingToRun || t.Status == TaskStatus.Running || t.Status == TaskStatus.Created);
19-
}
20-
21-
/// <summary>
22-
/// Returns a list of details about all tracked tasks, including their ID, status, and duration.
23-
/// </summary>
24-
/// <returns>A list of TaskInfo objects representing the tracked tasks.</returns>
25-
public List<TaskInfo> GetTaskDetails()
26-
{
27-
return _tasks.Values.ToList();
28-
}
29-
30-
/// <summary>
31-
/// Tracks the specified task, capturing its start time and status, and updates the information when the task completes.
32-
/// </summary>
33-
/// <param name="task">The task to track.</param>
34-
/// <returns>A TaskInfo object containing details about the task.</returns>
35-
public TaskInfo TrackTask(Task task)
7+
public class TaskManager : ITaskManager
368
{
37-
var taskInfo = new TaskInfo
38-
{
39-
TaskId = task.Id,
40-
Status = task.Status,
41-
StartTime = DateTime.Now
42-
};
439

44-
_tasks.TryAdd(task.Id, taskInfo);
10+
private readonly ConcurrentDictionary<int, TaskInfo> _tasks = new();
4511

46-
task.ContinueWith(t =>
12+
/// <summary>
13+
/// Retrieves the count of pending tasks, which are either waiting to run or currently running.
14+
/// </summary>
15+
/// <returns>The count of pending tasks.</returns>
16+
public int GetPendingTaskCount()
17+
{
18+
return _tasks.Values.Count(t => t.Status == TaskStatus.WaitingToRun || t.Status == TaskStatus.Running || t.Status == TaskStatus.Created);
19+
}
20+
21+
/// <summary>
22+
/// Returns a list of details about all tracked tasks, including their ID, status, and duration.
23+
/// </summary>
24+
/// <returns>A list of TaskInfo objects representing the tracked tasks.</returns>
25+
public List<TaskInfo> GetTaskDetails()
26+
{
27+
return _tasks.Values.ToList();
28+
}
29+
30+
/// <summary>
31+
/// Tracks the specified task, capturing its start time and status, and updates the information when the task completes.
32+
/// </summary>
33+
/// <param name="task">The task to track.</param>
34+
/// <returns>A TaskInfo object containing details about the task.</returns>
35+
public TaskInfo TrackTask(Task task)
4736
{
48-
taskInfo.Status = t.Status;
49-
taskInfo.EndTime = DateTime.Now;
50-
if (t.IsFaulted && t.Exception != null)
37+
var taskInfo = new TaskInfo
5138
{
52-
taskInfo.Exception = t.Exception;
53-
}
54-
});
39+
TaskId = task.Id,
40+
Status = task.Status,
41+
StartTime = DateTime.Now
42+
};
5543

56-
return taskInfo;
44+
_tasks.TryAdd(task.Id, taskInfo);
45+
46+
task.ContinueWith(t =>
47+
{
48+
taskInfo.Status = t.Status;
49+
taskInfo.EndTime = DateTime.Now;
50+
if (t.IsFaulted && t.Exception != null)
51+
{
52+
taskInfo.Exception = t.Exception;
53+
}
54+
});
55+
56+
return taskInfo;
57+
}
5758
}
58-
}
59+
60+
}
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9-
<DockerfileContext>..\..</DockerfileContext>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netstandard2.1;net9.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
<DockerfileContext>..\..</DockerfileContext>
1010

11-
12-
<PackageId>TaskMonitoring</PackageId>
13-
<Version>1.0.0</Version>
14-
<Authors>Yasin Asadnezhad</Authors>
15-
<Description>Task Monitoring is a C# library designed to monitor and manage Task and ThreadPool usage in .NET applications. It provides an API to expose runtime statistics, such as the number of active threads and tasks, and detailed task execution information.</Description>
16-
17-
<PackageProjectUrl>https://www.nuget.org/packages/TaskMonitoring/</PackageProjectUrl>
18-
<RepositoryType>git</RepositoryType>
19-
20-
<PackageTags>ThreadPool Task Monitoring NuGet</PackageTags>
21-
<RepositoryUrl>https://github.com/YAS-SIIN/TaskMonitoring</RepositoryUrl>
22-
</PropertyGroup>
2311

24-
<ItemGroup>
25-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
26-
</ItemGroup>
12+
<PackageId>TaskMonitoring</PackageId>
13+
<Version>1.0.0</Version>
14+
<Authors>Yasin Asadnezhad</Authors>
15+
<Description>Task Monitoring is a C# library designed to monitor and manage Task and ThreadPool usage in .NET applications. It provides an API to expose runtime statistics, such as the number of active threads and tasks, and detailed task execution information.</Description>
16+
17+
<PackageProjectUrl>https://www.nuget.org/packages/TaskMonitoring/</PackageProjectUrl>
18+
<RepositoryType>git</RepositoryType>
19+
20+
<PackageTags>ThreadPool Task Monitoring NuGet</PackageTags>
21+
<RepositoryUrl>https://github.com/YAS-SIIN/TaskMonitoring</RepositoryUrl>
22+
</PropertyGroup>
23+
24+
<ItemGroup>
25+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
26+
</ItemGroup>
2727

2828
</Project>

src/TaskMonitoring.Application/ThreadTaskMonitor.cs

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,53 @@
22

33
using TaskMonitoring.Application.Models;
44

5-
namespace TaskMonitoring.Application;
6-
7-
public class ThreadPoolMonitor : IThreadPoolMonitor
5+
namespace TaskMonitoring.Application
86
{
9-
/// <summary>
10-
/// Retrieves the total number of active threads in the thread pool.
11-
/// </summary>
12-
/// <returns>The current active thread count in the thread pool.</returns>
13-
public int GetActiveThreadCount()
7+
public class ThreadPoolMonitor : IThreadPoolMonitor
148
{
15-
return ThreadPool.ThreadCount;
16-
}
9+
/// <summary>
10+
/// Retrieves the total number of active threads in the thread pool.
11+
/// </summary>
12+
/// <returns>The current active thread count in the thread pool.</returns>
13+
public int GetActiveThreadCount()
14+
{
15+
// Fix: Replace ThreadPool.ThreadCount with a custom implementation
16+
ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads);
17+
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIoThreads);
1718

18-
/// <summary>
19-
/// Retrieves the current status of the thread pool, including the number of available worker and IO threads.
20-
/// </summary>
21-
/// <returns>A ThreadPoolStatus object containing the number of available worker and IO threads.</returns>
22-
public ThreadPoolStatus GetThreadPoolStatus()
23-
{
24-
ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads);
25-
return new ThreadPoolStatus { WorkerThreadsAvailable = workerThreads, IoThreadsAvailable = ioThreads };
26-
}
19+
int activeWorkerThreads = maxWorkerThreads - workerThreads;
20+
int activeIoThreads = maxIoThreads - ioThreads;
2721

28-
/// <summary>
29-
/// Sets the minimum number of threads that the thread pool maintains for worker and IO threads.
30-
/// </summary>
31-
/// <param name="threadPoolStatus">The minimum number of worker threads and the minimum number of IO threads.</param>
32-
public void SetMinThreads(ThreadPoolStatus threadPoolStatus)
33-
{
34-
ThreadPool.SetMinThreads(threadPoolStatus.WorkerThreadsAvailable, threadPoolStatus.IoThreadsAvailable);
35-
}
22+
return activeWorkerThreads + activeIoThreads;
23+
}
3624

37-
/// <summary>
38-
/// Sets the maximum number of threads that the thread pool can maintain for worker and IO threads.
39-
/// </summary>
40-
/// <param name="threadPoolStatus">The maximum number of worker threads and the maximum number of IO threads.</param>
41-
public void SetMaxThreads(ThreadPoolStatus threadPoolStatus)
42-
{
43-
ThreadPool.SetMaxThreads(threadPoolStatus.WorkerThreadsAvailable, threadPoolStatus.IoThreadsAvailable);
25+
/// <summary>
26+
/// Retrieves the current status of the thread pool, including the number of available worker and IO threads.
27+
/// </summary>
28+
/// <returns>A ThreadPoolStatus object containing the number of available worker and IO threads.</returns>
29+
public ThreadPoolStatus GetThreadPoolStatus()
30+
{
31+
ThreadPool.GetAvailableThreads(out int workerThreads, out int ioThreads);
32+
return new ThreadPoolStatus { WorkerThreadsAvailable = workerThreads, IoThreadsAvailable = ioThreads };
33+
}
34+
35+
/// <summary>
36+
/// Sets the minimum number of threads that the thread pool maintains for worker and IO threads.
37+
/// </summary>
38+
/// <param name="threadPoolStatus">The minimum number of worker threads and the minimum number of IO threads.</param>
39+
public void SetMinThreads(ThreadPoolStatus threadPoolStatus)
40+
{
41+
ThreadPool.SetMinThreads(threadPoolStatus.WorkerThreadsAvailable, threadPoolStatus.IoThreadsAvailable);
42+
}
43+
44+
/// <summary>
45+
/// Sets the maximum number of threads that the thread pool can maintain for worker and IO threads.
46+
/// </summary>
47+
/// <param name="threadPoolStatus">The maximum number of worker threads and the maximum number of IO threads.</param>
48+
public void SetMaxThreads(ThreadPoolStatus threadPoolStatus)
49+
{
50+
ThreadPool.SetMaxThreads(threadPoolStatus.WorkerThreadsAvailable, threadPoolStatus.IoThreadsAvailable);
51+
}
4452
}
45-
}
53+
}
54+

test/TaskMonitoring.UnitTest/TaskMonitoring.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

0 commit comments

Comments
 (0)