CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C-Sharp Programming Post questions, answers, and comments about C#.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old June 15th, 2006, 11:46 AM
    mizer mizer is offline
    Member
     
    Join Date: May 2005
    Posts: 98
    mizer is an unknown quantity at this point (<10)
    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.
    Reply With Quote
      #2    
    Old June 15th, 2006, 12:12 PM
    wildfrog's Avatar
    wildfrog wildfrog is offline
    Elite Member
     
    Join Date: Apr 2005
    Location: Norway
    Posts: 3,934
    wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)
    Re: Timer

    Quote:
    Originally Posted by mizer
    I am not sure how to use the timer class, and how to get it to fire an event everyday at midnight
    The is an example of how to use the Timer class here.

    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]
    Reply With Quote
      #3    
    Old June 15th, 2006, 01:34 PM
    MadHatter MadHatter is offline
    Senior Member
     
    Join Date: Mar 2004
    Location: 33°11'18.10"N 96°45'20.28"W
    Posts: 1,767
    MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)
    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
    Reply With Quote
      #4    
    Old June 15th, 2006, 01:59 PM
    mizer mizer is offline
    Member
     
    Join Date: May 2005
    Posts: 98
    mizer is an unknown quantity at this point (<10)
    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);
    Reply With Quote
      #5    
    Old June 15th, 2006, 02:26 PM
    wildfrog's Avatar
    wildfrog wildfrog is offline
    Elite Member
     
    Join Date: Apr 2005
    Location: Norway
    Posts: 3,934
    wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)wildfrog has a reputation beyond repute (3000+)
    Re: Timer

    Quote:
    Originally Posted by MadHatter
    actually the timer can do what he's wanting.
    You of course right. Just a comment, this solution will get out of sync if the system time is changed during execution. I'm not sure if there is a .NET way of detecting time changes, but you can always handle the Win32 APIs WM_TIMECHANGE message.

    - 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]
    Reply With Quote
      #6    
    Old June 15th, 2006, 02:49 PM
    MadHatter MadHatter is offline
    Senior Member
     
    Join Date: Mar 2004
    Location: 33°11'18.10"N 96°45'20.28"W
    Posts: 1,767
    MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)
    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:
    Originally Posted by mizer
    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);
    this is a time sync process. (which you can do continously inside your timer callback to stay sync'd even if the time changes... of course you might have to add the daylight savings change calculations on your own if its not built in).

    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");
    it creates a date time object that represents the current day at 12 am. since this is the exact time we want to process whatever it is we're doing, we get the time differential:

    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);
    actually it should (now thinking about it) be while(now >= syncTime) so that it continues to change the sync time by the interval unitl its in the future.

    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;
    then change the timer (which was previously disabled by passing in the -1, -1 so it didnt start until we wanted it to)

    Code:
    t.Change(startAt, interval)
    so in the amount of time specified in startAt, the timer will fire. after this, it uses the interval to trigger the next time.

    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.
    Reply With Quote
      #7    
    Old June 15th, 2006, 03:21 PM
    mizer mizer is offline
    Member
     
    Join Date: May 2005
    Posts: 98
    mizer is an unknown quantity at this point (<10)
    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.
    Reply With Quote
      #8    
    Old June 15th, 2006, 03:31 PM
    MadHatter MadHatter is offline
    Senior Member
     
    Join Date: Mar 2004
    Location: 33°11'18.10"N 96°45'20.28"W
    Posts: 1,767
    MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)
    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
    Reply With Quote
      #9    
    Old June 15th, 2006, 03:53 PM
    mizer mizer is offline
    Member
     
    Join Date: May 2005
    Posts: 98
    mizer is an unknown quantity at this point (<10)
    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.
    Reply With Quote
      #10    
    Old June 16th, 2006, 03:15 AM
    MadHatter MadHatter is offline
    Senior Member
     
    Join Date: Mar 2004
    Location: 33°11'18.10"N 96°45'20.28"W
    Posts: 1,767
    MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)MadHatter is a splendid one to behold (750+)
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > C-Sharp Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 08:39 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009