.net - C# Calling method every minute at 55 second mark -
I have been given a task to write a program to count how many page ideas have been requested from our site. The current approach is to get data from Google Analytics Real Time API, which works for my surprise
My problem is that I have to get a pweview of data every time from Google AP (this past Gives the amount of 29 minutes + Setting the value from a timer that reset every minute) After setting the 'reset talk', I would say, every minute in the 55th second, I voted 56th and later 53th place, which is relatively New users / page gives a relatively good estimate of ideas
So this is my current viewpoint:
stable system Tires Timer my timer = new system Times Timer (); MyTimer.AutoReset = false; MyTimer.Interval = Interval (); MyTimer.Elapsed + = myTimer_Elapsed2; MyTimer.Start (); Fixed Double Interval () {Returns 1000 - Datetime. Now.Millisecond; } Fixed zero myTimer_Elapsed2 (Object Sender, System.Timers.ElapsedEventArgs e) {if (DateTime.Now.Second == (Reset page. Second -%)% 60 & Warden) {DoStuff (); // Most APIs insert Google API data} and if (datetime.now.Second == (Reset page. Second + 1)% 60) // So we do not get above 60 and above {// Here are some data I use it later in DoStuff - to calculate the difference between most later} myTimer.Interval = interval (); // Because it takes about 0.5 seconds to execute DoStuff (), I have to recreate my timer. Start (); }
And it actually works well, as long as it stops after 2 hours, for now I do not know why (program runs, just timer It does not work)
How can I make it stable for a long time?
@I have been edited to give me a better understanding of what I'm actually doing.
@Erd CREDITS
I use two timers I am running every single minute cycle and sometimes the database written is crashing and I did not handle this exception properly. Logged me that Google API functions get a bit more data from time to time, due to which many threads occur. Event calls and throw an exception in handling my database data so that the timer can be stopped.
I tried to use the Quartz approach, but in the absence of human-friendship, it left me this library.
.NET timer is trustworthy, this is to stop working randomly for no apparent reason. will do it.
Most likely, your timer is throwing a few exceptions to event handlers, which is not because System.Timers.Timer
squash exceptions have been said as follows:
The timer component captures all exceptions that the event handler has thrown for the spreading event and suppresses this behavior, subject to change in the future release of the .NET framework.
Because of that behavior there is a little "subject to change" at least there. NET 2.0 is
Do I feel that this timer tells your event handler that one of the event handler or caller throws an exception, and the timer leaves it on the floor because you can not handle it.
Your MyTimer_Elapsed2
method so that you can at least log any exceptions that crop up, with the information given from the exception log, you can possibly identify it What are the problems that are.
Better still, stop using System.Timers.Timer
. Use it instead
Finally, there is no way that your code will give you a timer in just 55 seconds of minutes, every minute. The timer is not accurate It will stop some milliseconds in each minute, over time it starts running at 54 seconds (or maybe 56), and then 53 (or 57), etc. If you need this, then stay firmly within 55 seconds of the minute, then you will need to reset the timer after all the time, keeping the current time in mind.
I suspect that every minute needs to be checked properly on the 55-second mark Overkill just set your timer to last every minute, and then determine the exact time passed from the last tick. So a "minute" can be 61 or 62 seconds, and the second can be 58 or 59 seconds. If you accumulate the number of requests and the time elapsed, you can smooth the barrier in the subsequent processing and give you a reliable request-per minute number. Trying to collect data on precise One minute limitations can be extremely difficult, even if it is possible with non-real-time operating systems like Windows.
Comments
Post a Comment