| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C-Sharp Programming Post questions, answers, and comments about C#. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Timer
I have a static hashtable, and everyday at midnight I need to clear this hashtable and reload it.
I am not sure how to use the timer class, and how to get it to fire an event everyday at midnight to clear the hashtable and reload it. Some more details about my project: I have a Fill() method which will fill the hashtable. I was thinking of starting the timer when this method is called. This method should only be called once a day. I also have a refresh method to call to clear the hashtable, and it also calls my Fill method. Also this is a class library project, not a windows form project if that helps. If someone could be so kind to post some code as to how to do this I would appreciate it. As always thanks in advance. |
|
#2
|
||||
|
||||
|
Re: Timer
Quote:
The Timer class doesn't relate to time of day, it triggers at given intervalls. To make your function start every midnight you can make your timer trigger every minute or so. Then, in your handler function, you can add logics that checks the current date. If it's a new day, call your Fill method. - petter
__________________
I love deadlines. I like the whooshing sound they make as they pass by - Douglas Adams. Visit me!. Use code-tags! [code]Your code here[/code] |
|
#3
|
|||
|
|||
|
Re: Timer
actually the timer can do what he's wanting.
I just wrote a service that runs every <specified interval> at <given time>, and it works great. here's a sample: Code:
using System;
using System.Collections;
using System.Threading;
namespace TestBed {
public class Program {
string sync = "12:00 AM";
Hashtable ht = new Hashtable();
public Program() {
// create the timer
Timer t = new Timer(new TimerCallback(TimerMethod), null, -1, -1);
// our interval time span
TimeSpan interval = new TimeSpan(1,0,0,0,0);
// get our sync up details
DateTime syncTime = DateTime.Parse(sync);
DateTime now = DateTime.Now;
if(now > syncTime) syncTime = syncTime.Add(interval);
// time differential between now and the time we start
TimeSpan startAt = syncTime - now;
t.Change(startAt, interval);
// wait for ever
System.Diagnostics.Process.GetCurrentProcess().WaitForExit();
}
static void Main(string[] args) {
new Program();
}
void TimerMethod(object state) {
ht.Clear();
}
}
}
__________________
/bb|[^b]{2}/ my deeply padded corner of the internet |
|
#4
|
|||
|
|||
|
Re: Timer
Based on my original post, would you put that code in the Fill() method? Keep in mind the Fill() method will only be called once a day (barring a power failure, etc...).
Thanks again for the help. Also could you explain to me what is going on in this part of the code: Code:
if(now > syncTime) syncTime = syncTime.Add(interval);
// time differential between now and the time we start
TimeSpan startAt = syncTime - now;
t.Change(startAt, interval);
|
|
#5
|
||||
|
||||
|
Re: Timer
Quote:
- petter
__________________
I love deadlines. I like the whooshing sound they make as they pass by - Douglas Adams. Visit me!. Use code-tags! [code]Your code here[/code] |
|
#6
|
|||
|
|||
|
Re: Timer
it says i got to spread the love b4 i can rate you again wild. I hadnt considered the time change, but I'll have to look into the time change notification.
Quote:
I specified a time of 00:00 (or 12 am for us yanks) as my sync time. when I do: Code:
DateTime syncTime = DateTime,Parse("12:00 AM");
Code:
//if our sync time is in the past, lets alter it to the next time it should fire if(now > syncTime) syncTime = syncTime.Add(interval); now that we have our sync time in the future, we get the time span difference between right now and when it needs to fire: Code:
TimeSpan startAt = syncTime - now; Code:
t.Change(startAt, interval) if you specify -1 (milliseconds) for the interval, it will only run once (assuming you gave it a non -1 "dueTime"). if you wanted to constantly stay sync'd up, you could always alter the interval time inside the TimerCallback and call timer.Change passing in the right times. as for where to put the fill method, I'd have to say (ignorant of all the details for your project) yes. you may want to fill when the app starts, then maintain it when the timer elapses (again depending on what you're doing). my app just indexed a ton of data for our search engine, so it wasnt necessary to do that processing, when the app starts, but if you need data to be in the hashtable before your timer fires, then it may be a good idea to fill it when the app starts.
__________________
/bb|[^b]{2}/ my deeply padded corner of the internet Last edited by MadHatter; June 15th, 2006 at 03:03 PM. |
|
#7
|
|||
|
|||
|
Re: Timer
Thanks for the explanation.
Here are some details about my project. Basically I am writing a class library. When this class library is instantiated, it is passed some XML. I parse out some information I need. I then take that information and start pulling out information from the hashtable to do some comparisons. I check and see if the hashtable is empty, if it is, I call my Fill() method. There is a webservice running that will instantiate my class library each time a user sends us some XML to parse. I could get 10,000 requests a day, so the idea of hitting the DB once and storing the info in a hashtable was my bosses idea. My concern in putting this timer code in the Fill() method is say I am the first user of the day, so the Fill() method is called and my timer starts, once I give the user their info back does the timer go away cuz the thread that initially created it is done? Hopefully that made sense, I am not sure how to put it any other way. Maybe this will help: user1 sends an estimate in XML, hash is empty so fill it and start timer. i send user1 an XML response. user2 sends an estimate in XML, hash HAS data, i send user2 an XML response. etc.....Did I just lose my timer cuz I am done with user1? Thanks again for the help. |
|
#8
|
|||
|
|||
|
Re: Timer
I dont believe it will work in a webservice. thats just my ignorance of initialization and lifetime of webservices though. depending on where and how you initialize your object may make the difference of whether it works or not.
__________________
/bb|[^b]{2}/ my deeply padded corner of the internet |
|
#9
|
|||
|
|||
|
Re: Timer
Well to be honest, I haven't seen that side of the project yet (webservice side) so I really can't tell you how my object is being instantiated.
Could you make the timer static? Would that work then so I wouldn't lose it after the first user? Is there any examples of where this could work if I did instantiate it correctly; i.e. not sure how the code would look to instantiate my object correctly so this will work. |
|
#10
|
|||
|
|||
|
Re: Timer
I tested it w/ a singleton object, and it was inconsistent (I did it in .net 2.0). it appeared as if the timer worked for a little while, but stopped after some time.
for my test I used a singleton styled object that created a timer that ran every second, and cleared the hashtable when it elapsed. it refilled the HT w/ random data (& exactly 5 items). my web method would add 1 item to the hash table, so everytime I invoked the WM it printed out the time, thread id, item count, and listed the items in the HT. I'd have a new set of data everytime I refreshed the page (w/ 6 items) for like the first few seconds, but then it would stack up and appear as though the timer had bailed and my collection would continue to grow unchecked. good luck. I'm sure if you could find out what was causing the timer to bail, you could produce a consistent managed object.
__________________
/bb|[^b]{2}/ my deeply padded corner of the internet |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|