I am using C# and I want to create an event that display a field to the user after 24 hours after it is been filled up. Meaning when the field is filled up I have to wait 24 hours before I display it to the user.
Is there any simple way to do so?
I didn't write any code yes since examples I found on line where suggesting timers and it doesn't resemble my problem.
I'm using .NET 3.5
Create an event with start date as an input parameter and 24h forward
126 Views Asked by Mindan At
2
There are 2 best solutions below
3
On
This sample code (Console application) demonstrates when property value changed, then after 24 hour calls method to showing data:
public class Program
{
private CancellationTokenSource tokenSource;
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
_ = Run(() => SendToUser(_myProperty), TimeSpan.FromHours(24), tokenSource.Token);
}
}
private static void Main()
{
var prg = new Program();
prg.tokenSource = new CancellationTokenSource();
prg.MyProperty = "Test test";
Console.ReadLine();
}
private void SendToUser(string data)
{
//Display data for user
Console.WriteLine(data);
}
public static async Task Run(Action action, TimeSpan period, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await Task.Delay(period, cancellationToken);
if (!cancellationToken.IsCancellationRequested)
action();
}
}
public static Task Run(Action action, TimeSpan period)
{
return Run(action, period, CancellationToken.None);
}
}
In .NET 3.5:
or use FluentScheduler - ready nuget task scheduler package, https://www.nuget.org/packages/FluentScheduler/3.1.46