none
c# Current Time & Date function with Application Exit. RRS feed

  • Question

  • For example, i need script which would get the current time and day, lets say that today its 2018/09/05 15:00 PM, And i need to make that the app if its run on 2018/09/05 23:59 It will be working, but after minute when the data goes to 2018/09/05 will make that application.exit function. And the app will Close.


    • Edited by GeyBen420 Wednesday, May 9, 2018 11:58 AM
    Wednesday, May 9, 2018 11:58 AM

Answers

  • May This Help You

    Check for another solution

    1. Add static AppValidator class in the Project Namespace as:

        public static class AppValidator
        {
            private static System.Timers.Timer timer = new System.Timers.Timer();
            private static void handleTimer()
            {
                timer.Start();
                timer.Elapsed += (send, ex) =>
                {
                    timer.Stop();
                    ExitMsg();
                };
            }
            private static void ExitMsg()
            {
                MessageBox.Show("Time Limit Reached! Application will Exit Now!");
                Application.Exit();
            }
            public static void ValidateAppWith_Interval(TimeSpan TSpan)
            {
                timer.Interval = TSpan.TotalMilliseconds;
                handleTimer();
            }
            public static void ValidateAppWith_End(DateTime DateTimeEnd)
            {
                if (DateTimeEnd <= DateTime.Now)
                    ExitMsg();
                else
                {
                    timer.Interval = (DateTimeEnd - DateTime.Now).TotalMilliseconds;
                    handleTimer();
                }                
            }
        }

    2. Now you can use this class directly in your form class in 2 ways:

      a. Specify End DateTime

         E.g. If You want to close application on 13 May 2018, 00:00:00 then in form_load event use AppValidator.ValidateAppWith_End function:

    private void Form1_Load(object sender, EventArgs e)
            {
                //Create TimeLimit by Specifying Year, Month, Day, Hour, Minute, Second
                DateTime TimeLimit = new DateTime(2018,5,13,0,0,0);     // 13/05/2018 00:00:00
                AppValidator.ValidateAppWith_End(TimeLimit);
                MessageBox.Show("With Time Limit as on " + TimeLimit.ToString() + " the app will no longer work");  
            }
     Note that if TimeLimit is greater than CurrentTime application will wait otherwise application will close directly after MessageBox is Displayed.

    b. Specify TimeSpan

    E.g. If You want to close application in particular time limit (E.g. in 1 minute) from AppStartTime then in form_load event use AppValidator.ValidateAppWith_Interval function:

            private void Form1_Load(object sender, EventArgs e)
            {
                //Create TSpan with (Hour, Minute, Second) 
                //E.g. for 1 Minute TSpan it's (0,1,0)
                TimeSpan TSpan = new TimeSpan(0,1,0);
                AppValidator.ValidateAppWith_Interval(TSpan);
                MessageBox.Show("With Time Limit as on " + (DateTime.Now+TSpan).ToString() + " the app will no longer work");
            }




    • Edited by Vikram Manjare Saturday, May 12, 2018 7:37 PM
    • Marked as answer by GeyBen420 Saturday, May 12, 2018 9:50 PM
    Saturday, May 12, 2018 6:52 PM

All replies

  • Depends upon the type of app but there are a couple of ways of doing it. 

    1) When the app starts calculate the total time it should run (Desired end - current). Then set a timer to fire when it occurs.

    2) Set a timer to run every second and compare the time to the desired end time. Not very efficient but it works.

    In either case when the appropriate time has elapsed then close the main window or shut down the app depending upon the type of application you are working with.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 9, 2018 2:00 PM
    Moderator
  • Add timer field to form & handle Form Load Event as

        public partial class Form1 : Form
        {
            private static System.Timers.Timer timer = new System.Timers.Timer();
    
            public Form1()
            {
                InitializeComponent();
                this.Load += Form1_Load;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DateTime currentTime = DateTime.Now;
                DateTime nextDayStartTime = DateTime.Today.AddDays(1);
                double waitForMilliSeconds = (nextDayStartTime - currentTime).TotalMilliseconds;
                MessageBox.Show("Form Load Time: " + (currentTime.ToString("yyyy/dd/MM H:mm")));
                timer.Interval = waitForMilliSeconds;
                timer.Start();
                timer.Elapsed += (send, ex) =>
                 {
                     timer.Stop();
                     MessageBox.Show("Date Changed! Application will Exit Now!");
                     Application.Exit();
                 };
            }
        }


    Wednesday, May 9, 2018 2:50 PM
  • Bug: 

    When form loads, the Form load time shows 3 times, and can you make the script like the program opens at, lets say 8:40 today, and can it closes at 8:50

    Wednesday, May 9, 2018 3:35 PM
  • Probably something wrong in your code. Did you make it yourself or somebody else, because we see nothing. 

    Success
    Cor

    Wednesday, May 9, 2018 4:06 PM
  • When form loads, the Form load time shows 3 times, and can you make the script like the program opens at, lets say 8:40 today, and can it closes at 8:50

    Does Messagebox show 3 times? The line was just provided to show the current time and day as per your question. Just comment the line to avoid it.

    About second question:

    You can do it by using timers, see Example at link below & modify it as per your need

    E.g. Set Timer Interval
            // Create a timer with a 10 min= (10*60) second=600*1000 millsecond interval.
            aTimer = new System.Timers.Timer(600000);
            

    Take a look at:

    https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx#Examples






    Wednesday, May 9, 2018 4:11 PM
  • Well,

    that script is really bugged, may you create a one, i will say how it should work.

    I have few forms, and how it may work on them, first one is login and register, the other are other stuff.

    So the script should work like, when you run the app on the first form will be a message "2018/05/10 the app will no longer work". And just when the script gets the time, it will exit in the 2018/05/10

    Wednesday, May 9, 2018 6:16 PM
  • When is that time fixed in the app? Point of login, first use, install time? Does it ever reset? For example when the app closes and the user opens it back up does the time start over? 

    It sounds like you're trying to build in some sort of temporal based licensing. My personal recommendation is to forego your own implementation and simply use an off the shelf solution. In most cases your approach isn't going to be that secure.

    If you just want to limit the amount of time a single run of your app can go then a timer at startup with an expiration of whatever time you specify is sufficient. It would be reset each time the app runs.

    If you're trying to build a trial limit (e.g. can only run the app for 30 days from installation) then the time would need to be captured in a secure location when it is installed. Thereafter each time the app is started just check the time. You don't really need to continually check unless you really, really want to limit the user. Most users aren't going to be running your app for days on end so it shouldn't matter if they "get" an extra day. It is also the simplest solution.

    Any questions related to how a UI should work should be posted in the appropriate UI forum. This question should be limited to your original question of how to detect when an elapsed time has occurred and close the app. Thanks.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 9, 2018 6:32 PM
    Moderator
  • Well,

    About my app is that i did not connected to mysql database cuz i dont have host, so i cant add users to app, so i need to update it daily.

    Wednesday, May 9, 2018 7:33 PM
  • For example, i need script which would get the current time and day, lets say that today its 2018/09/05 15:00 PM, And i need to make that the app if its run on 2018/09/05 23:59 It will be working, but after minute when the data goes to 2018/09/05 will make that application.exit function. And the app will Close. The thing why i need that is i don't have database, so i need to update app manual. Just get the time and check if its not outdated, if yes, application.exit
    Saturday, May 12, 2018 8:40 AM
  • Saturday, May 12, 2018 8:54 AM
  • With no right answers 
    Saturday, May 12, 2018 10:27 AM
  • The first answer from Vikram Manjare is fine

    A simple test by changing the elapsed time to 1 mn to check it quickly shows that it works.

    Saturday, May 12, 2018 10:46 AM
  • With no right answers 
    Explain what is: "a right answer". If you make a program like this, it does not even show a window. 

    Success
    Cor

    Saturday, May 12, 2018 11:39 AM
  • May This Help You

    Check for another solution

    1. Add static AppValidator class in the Project Namespace as:

        public static class AppValidator
        {
            private static System.Timers.Timer timer = new System.Timers.Timer();
            private static void handleTimer()
            {
                timer.Start();
                timer.Elapsed += (send, ex) =>
                {
                    timer.Stop();
                    ExitMsg();
                };
            }
            private static void ExitMsg()
            {
                MessageBox.Show("Time Limit Reached! Application will Exit Now!");
                Application.Exit();
            }
            public static void ValidateAppWith_Interval(TimeSpan TSpan)
            {
                timer.Interval = TSpan.TotalMilliseconds;
                handleTimer();
            }
            public static void ValidateAppWith_End(DateTime DateTimeEnd)
            {
                if (DateTimeEnd <= DateTime.Now)
                    ExitMsg();
                else
                {
                    timer.Interval = (DateTimeEnd - DateTime.Now).TotalMilliseconds;
                    handleTimer();
                }                
            }
        }

    2. Now you can use this class directly in your form class in 2 ways:

      a. Specify End DateTime

         E.g. If You want to close application on 13 May 2018, 00:00:00 then in form_load event use AppValidator.ValidateAppWith_End function:

    private void Form1_Load(object sender, EventArgs e)
            {
                //Create TimeLimit by Specifying Year, Month, Day, Hour, Minute, Second
                DateTime TimeLimit = new DateTime(2018,5,13,0,0,0);     // 13/05/2018 00:00:00
                AppValidator.ValidateAppWith_End(TimeLimit);
                MessageBox.Show("With Time Limit as on " + TimeLimit.ToString() + " the app will no longer work");  
            }
     Note that if TimeLimit is greater than CurrentTime application will wait otherwise application will close directly after MessageBox is Displayed.

    b. Specify TimeSpan

    E.g. If You want to close application in particular time limit (E.g. in 1 minute) from AppStartTime then in form_load event use AppValidator.ValidateAppWith_Interval function:

            private void Form1_Load(object sender, EventArgs e)
            {
                //Create TSpan with (Hour, Minute, Second) 
                //E.g. for 1 Minute TSpan it's (0,1,0)
                TimeSpan TSpan = new TimeSpan(0,1,0);
                AppValidator.ValidateAppWith_Interval(TSpan);
                MessageBox.Show("With Time Limit as on " + (DateTime.Now+TSpan).ToString() + " the app will no longer work");
            }




    • Edited by Vikram Manjare Saturday, May 12, 2018 7:37 PM
    • Marked as answer by GeyBen420 Saturday, May 12, 2018 9:50 PM
    Saturday, May 12, 2018 6:52 PM
  • Thanks, now it does work. !!
    Saturday, May 12, 2018 9:50 PM
  • But what if the app is running and it comes to day 13, will it close?
    Saturday, May 12, 2018 9:54 PM
  • It should definitely close.

    If you have specified TimeLimit as

    DateTime TimeLimit = new DateTime(2018,5,13,0,0,0);     // 13/05/2018 00:00:00

    Then a running application started before TimeLimit will close after 13/05/2018 00:00:00.

    Sunday, May 13, 2018 3:29 AM