locked
How to test a TIMER RRS feed

  • Question

  • Hi, i have a program executing some timers every X time.  For example, a timer must be executed in 40 days, other in hours....

    My problem is, how can i to test if timer is executed correctly? If i change system clock from today to 39 days later, timer countdown is the same, it doesnt change.... then... i dont know how to test it.

    A solution is NOT divide the time TIMER, changing 60 hours in 60 seconds and simulate 1 hour = 1 day. I need test it when a new day or a month is reached, leap-years....

    Any way to do it?

    thanks in advance.

    Wednesday, March 21, 2012 5:39 AM

Answers

  • It doesn`t matter of a leap year, or anytihng, you set to the timer a time that he has to fire an event. Every timer has a Interval property, which is in milliseconds. 

    But there is another problem, if this application will be closed and re-open during this 40 days period, you have to think of some other solution. Timer will only start running and running until reached the Interval set time, if you will not close application (and not shutdown the pc). Is this case you have to create some file on hdd, where you will 1st write some value (txt file, example: expiredDate = 31.4.2012 14:00:00).

    SO every time you will start the application, the code will check for this file, and if the time is reached. If it is, it has to rise some event and notify the user of that timer (could be right after starting the application, or during the work with this application).


    Mitja

    • Proposed as answer by Neddy Ren Thursday, March 22, 2012 7:03 AM
    • Marked as answer by Neddy Ren Monday, March 26, 2012 6:03 AM
    Wednesday, March 21, 2012 6:00 AM
  • One solution is to have a timer ticking every day and there is a check for the particular date.

    or

    Creating a scheduled task for the particular day is also recommended


    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Proposed as answer by Neddy Ren Thursday, March 22, 2012 7:04 AM
    • Marked as answer by Neddy Ren Monday, March 26, 2012 6:03 AM
    Wednesday, March 21, 2012 9:28 AM
  • Hi SankB,

    So long time for the timer is not suggested. 

    I think you can use Windows schedule to achieve that, such as the article here: 

    http://support.microsoft.com/kb/308569 

    For more about the Task Scheduler, see the article here:

     

    Task Scheduler: 
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa383614.aspx

    Best Regards


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Neddy Ren Monday, March 26, 2012 6:03 AM
    Thursday, March 22, 2012 7:18 AM

All replies

  • It doesn`t matter of a leap year, or anytihng, you set to the timer a time that he has to fire an event. Every timer has a Interval property, which is in milliseconds. 

    But there is another problem, if this application will be closed and re-open during this 40 days period, you have to think of some other solution. Timer will only start running and running until reached the Interval set time, if you will not close application (and not shutdown the pc). Is this case you have to create some file on hdd, where you will 1st write some value (txt file, example: expiredDate = 31.4.2012 14:00:00).

    SO every time you will start the application, the code will check for this file, and if the time is reached. If it is, it has to rise some event and notify the user of that timer (could be right after starting the application, or during the work with this application).


    Mitja

    • Proposed as answer by Neddy Ren Thursday, March 22, 2012 7:03 AM
    • Marked as answer by Neddy Ren Monday, March 26, 2012 6:03 AM
    Wednesday, March 21, 2012 6:00 AM
  • i had all "problems" provided.

    The problem is that when a timer is reached, it must be relaunched with other interval that my program configure. Then i want to test if when a timer is over, it is relaunched properly....

    And if i want that process been launched in 1th march, i want to know if timer is actually launched that day (program before did some calculations to put the correct interval time of the timer, and i want to test that stuffs).

    But i dont know hot to do it :S

    Wednesday, March 21, 2012 7:50 AM
  • One solution is to have a timer ticking every day and there is a check for the particular date.

    or

    Creating a scheduled task for the particular day is also recommended


    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Proposed as answer by Neddy Ren Thursday, March 22, 2012 7:04 AM
    • Marked as answer by Neddy Ren Monday, March 26, 2012 6:03 AM
    Wednesday, March 21, 2012 9:28 AM
  • Problem is that execution date is not only limited to days, but hours (example:  30 march - 08:30 )

    Wednesday, March 21, 2012 9:36 AM
  • Okay.. Then a weird logic for timer to tick exactly at 29 Feb 2016 at 8:30 PM would be:

    TimeSpan span = new DateTime(2016, 2, 29, 20, 30, 0) - DateTime.Today;
    long delay = (long)span.TotalMilliseconds;

    But it won't work as the program is not expected to run upto that:

    If you insist on timer, I can say each time the application gets restarted:

    1) Set the timer to the next event time or 1 hour which is less.

       eg: if next event date is after 2 days, set for 1 hour.

    2) If the event time is less than 1 hour, adjust the delay accordingly

    Repeat the process in timer event too. (so the delay gets smaller and smaller based on the next event time.)


    Resolving n Evolving in C# (http://jeanpaulva.com)


    Wednesday, March 21, 2012 9:45 AM
  • Hi,

    That is ? You can also choose the hour on which it will happen. If the pattern is not regular, this is perhaps not possible with a single task but you could perhaps cover them all using multiple tasks each taking care of a particular pattern.

    I would really double check this standard OS features before rolling my own scheduling application. Your code could also schedule itself the next run. For example by running the AT command.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, March 21, 2012 9:48 AM
  • well... my program was finished.  I have a timer associated to every process.  Interval of every timer is milliseconds until execution date of that process.  And when proccess is executed timer time is restarted to execute that proccess again in the next date.

    And it work fine, but now i want to test it not only to the next minutes or hours to execute. I want to test my program when the proccess must to be executed in days or months (there is proccess that are executed every 2 or 3 days, 2 or 3 months.. ). But i cant wait month to know if my program work fine when timer is so long :\

    Wednesday, March 21, 2012 10:08 AM
  • Read posts mate. I have already explained you how to use a long time period timer.

    Iam sure this application of yours will be closed (or shuted down) during this time, so setting the timer only in your application will simply not do.

    Thats why you have to rely on some additional help, like creating a simple text file, and write into it the dead-line. And when opening application every time read this file and check if date has expired yet. Is so, rise the event.

    This is one of options.


    Mitja

    Wednesday, March 21, 2012 10:26 AM
  • Man i know that, my program recalculate timer time when program is restarted,  i have all "mechanism" done. Now i only want to test my program.
    Wednesday, March 21, 2012 10:37 AM
  • So set timer to interval of few minutes (same time write to text file), restart your computer and run again your application. 

    You will see if it rise the event that it should.

    PS: if it runs ones in a short period of time will rise again in a long period. As simple as that. Math does not let us down :)


    Mitja


    • Edited by Mitja Bonca Wednesday, March 21, 2012 10:45 AM
    Wednesday, March 21, 2012 10:44 AM
  • but to calculate times i have many algorithms to calculate milliseconds when the next launch date is after a month with 28-29-30 or 31 days and other situations... case it i need go to the future date (forwarding system time) and check is proccess is executed correctly

    I could simply look at milliseconds after it is calculated and calculate manually if time that time calculated by me is same that method return time.  But i wanted test it with a "real situation", but if i advance system time and timer time dont advance too, then i suppose that test it in this way is not possible

    Thanks anyway and sorry about my english xD

    Wednesday, March 21, 2012 10:57 AM
  • In example that Iam talking about, you have nothing to do with milliseconds any longer. This will be a so-called Custom Timer.

    You only set some date and time (ie: 21.4.2012 13:25 - this is in a month and 15 minutes app.), and when you only check this time. You dont deal with predefined timers from Visual Studio. 


    Mitja

    Wednesday, March 21, 2012 12:10 PM
  • It should if you launch your app after having changed your computer clock. A timer counts  a delay so if you change the system clock after the timer has been scheduled for in x days, it won't have any effect.

    You could also test separately if the calculation is correct. For example if you have a routine that takes a current and target data as a parameter,you could easily test that this function produces the right value in hardcoded test (http://en.wikipedia.org/wiki/Unit_testing) withiout having to test for days. The work that is done coudl be tested separately.

    It reminds me that I have an app that depends on external data usually at the end of the month. I just have scheduled a Windows tasks each day and this is the app that sees if it has fresh data to process...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, March 21, 2012 2:04 PM
  • Hi SankB,

    So long time for the timer is not suggested. 

    I think you can use Windows schedule to achieve that, such as the article here: 

    http://support.microsoft.com/kb/308569 

    For more about the Task Scheduler, see the article here:

     

    Task Scheduler: 
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa383614.aspx

    Best Regards


    Neddy Ren[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Neddy Ren Monday, March 26, 2012 6:03 AM
    Thursday, March 22, 2012 7:18 AM