Skip to content
Shiny Controls v1.0 - The Ultra Control Suite for .NET MAUI & BlazorO...M...G!

Refresh Timer

There are scenarios where you want to have data requests be called every so often. The RefreshTimer middleware is designed to help you with this.

public class MyRequest : IStreamRequest<string>;
public class MyStreamRequestHandler : IStreamRequestHandler<MyRequest, string>
{
[RefreshTimer(3000)] // every 3 seconds
public async IAsyncEnumerable<string> Handle(MyRequest request, IMediatorContext context, [EnumeratorCancellation] CancellationToken cancellationToken)
{
yield return DateTimeOffset.Now.ToString("h:mm:ss tt");
}
}

and now in your code, you can simply call the mediator to get the stream and await foreach

```csharp
var stream = mediator.Request(new MyRequest());
await foreach (var item in stream)
{
// this code will be called every 3 seconds
}

Much like other middleware, you can use the Microsoft.Extensions.Configuration.

{
"Mediator": {
"TimerRefresh": {
"MyNamespace.MyStreamHandler": {
"IntervalSeconds": 20
}
}
}
}