Search code examples
c#datetimetimersystemevent

Create an event with start date as an input parameter and 24h forward


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


Solution

  • In .NET 3.5:

    public class Program
    {
        private string _myProperty;
        public string MyProperty
        {
            get
            {
                return _myProperty; 
            }
            set
            {
                _myProperty = value;
                ExecuteAfter(() => SendToUser(_myProperty), TimeSpan.FromHours(24));
            }
        }
        private static void Main()
        {
            var prg = new Program();
            prg.MyProperty = "Test test";
            Console.ReadLine();
        }
        private void SendToUser(string data)
        {
            //Display data for user
            Console.WriteLine(data);
        }
        public static void ExecuteAfter(Action action, TimeSpan delay)
        {
            Timer timer = null;
            timer = new System.Threading.Timer(s =>
            {
                action();
                timer.Dispose();
             }, null, (long)delay.TotalMilliseconds, Timeout.Infinite);
        }
    }
    

    or use FluentScheduler - ready nuget task scheduler package, https://www.nuget.org/packages/FluentScheduler/3.1.46