TaskMonitoring 1.0.0

dotnet add package TaskMonitoring --version 1.0.0                
NuGet\Install-Package TaskMonitoring -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TaskMonitoring" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TaskMonitoring --version 1.0.0                
#r "nuget: TaskMonitoring, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TaskMonitoring as a Cake Addin
#addin nuget:?package=TaskMonitoring&version=1.0.0

// Install TaskMonitoring as a Cake Tool
#tool nuget:?package=TaskMonitoring&version=1.0.0                

Task Monitoring Library

Overview

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.

This library is useful for debugging, performance monitoring, and gaining insights into how tasks and threads are managed in your application.

Features

  • Track the number of currently active ThreadPool threads.
  • Monitor the number of scheduled, running, and completed tasks.
  • Expose detailed information about tasks, including their status, start time, and duration.
  • Provides an API to access task monitoring data, which can be extended to visualize metrics in charts.

Installation

You can install this package from NuGet using the .NET CLI:

dotnet add package TaskMonitoring

Or, through the NuGet Package Manager in Visual Studio:

  • Right-click on your project in Solution Explorer.
  • Select Manage NuGet Packages.
  • Search for TaskMonitoring.
  • Click Install.

Usage

Monitoring ThreadPool and Tasks To monitor thread and task activity, you can use the ThreadTaskMonitor class, which provides methods to track task execution and thread pool status.

Example:

using TaskMonitoring;

var monitor = new ThreadTaskMonitor();

// Get the current number of active ThreadPool threads
var activeThreads = monitor.GetActiveThreadCount();
Console.WriteLine($"Active ThreadPool Threads: {activeThreads}");

// Track a task and get detailed task info
var task = Task.Run(() => {
    Thread.Sleep(1000); // Simulate some work
});

monitor.TrackTask(task);

// Get task details
var taskDetails = monitor.GetTaskDetails();
foreach (var taskInfo in taskDetails)
{
    Console.WriteLine($"Task ID: {taskInfo.TaskId}, Status: {taskInfo.Status}");
}

Custom Task Scheduler

The library includes a custom TaskScheduler called MonitoringTaskScheduler that tracks tasks as they are scheduled and executed.

Example:

var monitoringScheduler = new MonitoringTaskScheduler();
var taskFactory = new TaskFactory(monitoringScheduler);

// Start a task using the custom task scheduler
taskFactory.StartNew(() => {
    Thread.Sleep(1000); // Simulate task work
});

taskFactory.StartNew(() => {
    Thread.Sleep(500); // Another task
});
// Get the count of scheduled tasks
int scheduledTaskCount = monitoringScheduler.GetScheduledTaskCount();
Console.WriteLine($"Currently scheduled tasks: {scheduledTaskCount}");

Contributing

Contributions are welcome! If you would like to contribute, please follow these steps:

Fork the repository.

  • Create a new feature branch (git checkout -b feature/your-feature-name).
  • Commit your changes (git commit -m 'Add some feature').
  • Push to the branch (git push origin feature/your-feature-name).
  • Create a pull request.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 112 10/20/2024