https://github.com/ginger-code/pollwatcher
Register handler functions to polling methods
https://github.com/ginger-code/pollwatcher
Last synced: about 1 year ago
JSON representation
Register handler functions to polling methods
- Host: GitHub
- URL: https://github.com/ginger-code/pollwatcher
- Owner: ginger-code
- License: mit
- Created: 2021-01-20T05:02:37.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-20T05:22:43.000Z (over 5 years ago)
- Last Synced: 2025-02-10T22:37:10.745Z (over 1 year ago)
- Language: C#
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PollWatcher
PollWatcher provides a way to register handler delegates to poll-based monitoring solutions.
This enables event-driven development in scenarios where events are not provided and cannot easily be added.
Possible use-cases include: polling a remote file server for changes, monitoring responses from an API, creating a recurring database task, etc.
Built leveraging the System.Reactive namespace
Example (C# 9.0 syntax):
```c#
internal static class Program
{
private static void Main()
{
using var watcher = new PollWatcher(GetDate, interval, PrintDate, PrintException);
// using var asyncWatcher = new AsyncPollWatcher(GetDateAsync, interval, PrintDate, PrintException);
Thread.Sleep(5000);
}
// Returns a value that changes over time but must be retrieved synchronously
// In this case, it's the time
private static DateTime GetDate() => DateTime.Now;
// Returns a value that changes over time but must be retrieved asynchronously
// In this case, it's the time
private static async Task GetDateAsync() => await Task.Run(() => DateTime.Now);
//Specifies polling should occur every .001ms (if possible)
private static TimeSpan interval = TimeSpan.FromMilliseconds(.001);
// "Does stuff" with the data retrieved from polling
// In this case, we print it to the console
private static void PrintDate(DateTime dateTime) => Console.WriteLine(dateTime.ToString("O"));
// "Does stuff" with exceptions raised during polling
// In this case, we print it to the console
private static void PrintException(Exception exception) => Console.WriteLine($"Exception!!!\t{exception.Message}");
}
```