pvWay.agentPoolManager.nc6 2.0.0

dotnet add package pvWay.agentPoolManager.nc6 --version 2.0.0
NuGet\Install-Package pvWay.agentPoolManager.nc6 -Version 2.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="pvWay.agentPoolManager.nc6" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pvWay.agentPoolManager.nc6 --version 2.0.0
#r "nuget: pvWay.agentPoolManager.nc6, 2.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 pvWay.agentPoolManager.nc6 as a Cake Addin
#addin nuget:?package=pvWay.agentPoolManager.nc6&version=2.0.0

// Install pvWay.agentPoolManager.nc6 as a Cake Tool
#tool nuget:?package=pvWay.agentPoolManager.nc6&version=2.0.0

PvWay Agent Pool Manager for dotNet Core 6

Manages a pool of background services (agents) that keep repeating at a given interval until they are requested to stop.

This lets your Web application runs a pool of tasks in the background.

Interfaces

This nuGet has only one public class implementing the following interface

IAgentPoolManager

namespace pvWay.agentPoolManager.nc6.Abstractions;

public interface IPvWayAgentPoolManager
{
    IEnumerable<IPvWayAgentPoolManagerAgent> Agents { get; }

    IPvWayAgentPoolManagerAgent? GetAgent(Guid id);

    IPvWayAgentPoolManagerAgent StartAgent<T>(
        string title,
        Action<T> repeat,
        T workerParam,
        TimeSpan sleepSpan,
        ThreadPriority priority = ThreadPriority.Normal,
        Action<IPvWayAgentPoolManagerAgent>? stopCallback = null);
    
    IPvWayAgentPoolManagerAgent StartAgent(
        string title,
        Action repeat,
        TimeSpan sleepSpan,
        ThreadPriority priority = ThreadPriority.Normal,
        Action<IPvWayAgentPoolManagerAgent>? stopCallback = null);
    
}

IAgent

namespace pvWay.agentPoolManager.nc6.Abstractions;

public interface IPvWayAgentPoolManagerAgent
{
    Guid Id { get; }
    DateTime StartTimeUtc { get; }
    string Title { get; }
    void RequestToStop();
}

Injection & factory

using Microsoft.Extensions.DependencyInjection;
using pvWay.agentPoolManager.nc6.Abstractions;
using pvWay.agentPoolManager.nc6.Impl;

namespace pvWay.agentPoolManager.nc6;

public static class PvWayAgentPoolManager
{
    public static void AddPvWayAgentPoolManager(
        this IServiceCollection services,
        ServiceLifetime lifetime = ServiceLifetime.Singleton)
    {
        var sd = new ServiceDescriptor(
            typeof(IPvWayAgentPoolManager), 
            typeof(PoolManager),
            lifetime);
        services.Add(sd);
    }

    public static IPvWayAgentPoolManager Create() => new PoolManager();
}

Usage

See here after a short Console that uses the pool

Principe

  • Create a method (with or without parameter) that you want to repeatedly invoke in background
  • Determine the interval of time between two invocations of your method
  • Instantiate the PoolManager
  • Add your method into the Agent Pool and in return get a IAgent reference
  • Stop the method at any time by calling the IAgent RequestToStop method

The following example shows the code for a simple clock pulsar that write the time in the console every 5 seconds.

The code

using pvWay.agentPoolManager.nc6;

Console.WriteLine("Hello, AgentPool");

var apm = PvWayAgentPoolManager.Create();

var pulsar = apm.StartAgent(
    // the name of the asynchronous agent
    "pulsar",
    // the method to repeat asynchronously
    Pulsar,
    // the string param passed to the Pulsar method
    "clock",
    // time between each invocation
    TimeSpan.FromSeconds(5),
    // the priority
    ThreadPriority.Normal,
    // the lambda that is called when the pulsar is stopped
    agent => Console.WriteLine($"{agent.Title} is stopped"));

Console.WriteLine("hit a key to stop");
Console.ReadKey();

pulsar.RequestToStop();

return;

static void Pulsar(string prefix)
{
    Console.WriteLine($"{prefix}-{DateTime.Now:HH:mm:ss}");
}

Happy coding

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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
2.0.0 103 1/25/2024
1.0.1 73 1/25/2024
1.0.0 268 4/7/2023

Dependency injection and factory