Skip to content

Repository files navigation

ktsu.IntervalAction

A .NET library that provides a simple way to execute an action at a specified interval.

LicenseNuGet VersionNuGet VersionNuGet DownloadsGitHub commit activityGitHub contributorsGitHub Actions Workflow Status

Introduction

IntervalAction is a .NET library that provides a simple and flexible way to execute an action at a specified interval. It offers precise control over execution timing, various interval types, and easy management of scheduled actions. Designed for scenarios where you need recurring tasks like polling, background processing, or timed updates.

Features

  • Flexible Timing: Execute actions at precise intervals
  • Interval Types: Choose between running intervals from last start or last completion
  • Configurable Polling: Fine-tune polling frequency for better performance
  • Immediate Execution: Option to execute actions immediately on start
  • Start/Stop Control: Easily pause and restart scheduled actions
  • Thread Safety: Safe for concurrent access from multiple threads
  • Low Overhead: Minimal resource usage during idle periods
  • Supports .NET 8.0 and .NET 9.0: Modern .NET support

Installation

Package Manager Console

Install-Package ktsu.IntervalAction

.NET CLI

dotnet add package ktsu.IntervalAction

Package Reference

<PackageReferenceInclude="ktsu.IntervalAction"Version="x.y.z" />

Usage Examples

Basic Example

usingktsu.IntervalAction;varintervalAction=IntervalAction.Start(new(){Action=()=>Console.WriteLine("Hello, World!"),ActionInterval=TimeSpan.FromSeconds(1),PollingInterval=TimeSpan.FromMilliseconds(100),// Optional, default is 1 secondIntervalType=IntervalType.FromLastStart// Optional, default is FromLastCompletion});// An action will execute immediately and then every second// outputting "Hello, World!" to the console until stoppedintervalAction.Stop();// The action will no longer execute until you call Restart()intervalAction.Restart();

Using FromLastCompletion Interval Type

usingktsu.IntervalAction;varintervalAction=IntervalAction.Start(new(){Action=()=>{Console.WriteLine($"Action started at: {DateTimeOffset.Now}");// Simulate a task that takes some time to completeTask.Delay(500).Wait();Console.WriteLine($"Action completed at: {DateTimeOffset.Now}");},ActionInterval=TimeSpan.FromSeconds(2),PollingInterval=TimeSpan.FromMilliseconds(100),IntervalType=IntervalType.FromLastCompletion// Waits until the action completes before starting the interval timer});// The action will execute immediately and then every 2 seconds after the previous execution completes

Handling Long-Running Tasks

usingktsu.IntervalAction;varintervalAction=IntervalAction.Start(new(){Action=async()=>{Console.WriteLine("Starting long-running task...");// Simulate a long-running taskawaitTask.Delay(5000);Console.WriteLine("Long-running task completed");},ActionInterval=TimeSpan.FromSeconds(10),IntervalType=IntervalType.FromLastCompletion// Important for long-running tasks});// For cleanup when doneawaitTask.Delay(60000);// Run for 1 minuteintervalAction.Stop();

Dynamic Interval Adjustment

usingktsu.IntervalAction;// Create with initial settingsvaroptions=newIntervalActionOptions{Action=()=>Console.WriteLine($"Tick at {DateTime.Now}"),ActionInterval=TimeSpan.FromSeconds(5)};varintervalAction=IntervalAction.Start(options);// Later, adjust the intervalawaitTask.Delay(20000);// After 20 secondsintervalAction.Stop();options.ActionInterval=TimeSpan.FromSeconds(1);intervalAction.Restart();Console.WriteLine("Interval changed to 1 second");

API Reference

IntervalAction Class

The main class for scheduling recurring actions.

Properties

NameTypeDescription
IsRunningboolIndicates if the action is currently scheduled to run
OptionsIntervalActionOptionsThe current options for this interval action

Methods

NameParametersReturn TypeDescription
StartIntervalActionOptions optionsIntervalActionStatic factory method to create and start an interval action
StopvoidStops the scheduled action from running
RestartvoidRestarts a previously stopped action
DisposevoidCleans up resources and stops the action

IntervalActionOptions Class

Configuration options for an interval action.

Properties

NameTypeDescription
ActionActionThe action to execute at intervals (required)
ActionIntervalTimeSpanThe interval between executions (required)
PollingIntervalTimeSpanHow frequently to check if action should run (optional, default 1 second)
IntervalTypeIntervalTypeDetermines how intervals are measured (optional, default FromLastCompletion)

IntervalType Enum

Defines how the interval is measured.

ValueDescription
FromLastCompletionThe interval starts after the action has completed
FromLastStartThe interval starts when the action begins executing

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to update tests as appropriate and adhere to the existing coding style.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Versioning

Check the CHANGELOG.md for detailed release notes and version changes.

Acknowledgements

Special thanks to all contributors and the .NET community for their support.

About

A .NET library that provides a simple way to execute an action at a specified interval with precise timing control.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages