Solved

Class CountDown TimeSpan and Timer?

Posted on 2006-06-15
4
1,174 Views
Last Modified: 2008-02-01
I'm attempting to fire an event every 24 hours with the following code.

I would use a simple Timer, but the Interval would be 1440000.........

Q. Is there a more simple approach than my home-brew code found below?


 private void CountDownTimer()
        {
            Timer timer = new Timer();
            timer.Tick += new EventHandler(TimerTick);
            timer.Interval = 60000;  // 60 Seconds
            timer.Start();
        }

        public static void TimerTick(object source, System.EventArgs e)
        {            
            DateTime LeaseDateTime = DateTime.Now.AddHours(24);
            TimeSpan TimeLeft = LeaseDateTime.Subtract(DateTime.Now);

            if (TimeLeft == 0/0/000)
            {
                // Run another method..........
            }
        }
0
Comment
Question by:kvnsdr
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 16916742
If you want to poll then something like...

        private DateTime targetTime;

        private void CountDownTimer()
        {
            targetTime = DateTime.Now.AddDays(1);
            Timer timer = new Timer();
            timer.Tick += new EventHandler(TimerTick);
            timer.Interval = 60000;  // 60 Seconds
            timer.Start();
        }

        public void TimerTick(object source, System.EventArgs e)
        {
            TimeSpan ts = targetTime.Subtract(DateTime.Now);
            if (ts.TotalMilliseconds <= 0)
            {
                // schedule next targeTime 24 hours out
                targetTime = DateTime.Now.AddDays(1);

                // run some method in here...

            }
        }
0
 
LVL 2

Expert Comment

by:kmaicorp
ID: 16917004
If you dont care about polling, why not just make the thread sleeps for 24 hours? Did you wanted to cancel the task before the 24 hours?
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 16917288
I'm simply looking for the best overall solution.

I use polling because of habit more than anything.

Although Thread seems like the most simple approach.

Thread.Sleep(1440000);
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16917353
Sleep() is a good approach.  Encapsulate a thread in a Class and put that thread to sleep so it doesn't lock up your GUI.  Then make the class raise a custom event to notify the main UI when to do something.  Be sure to marshal the threaded event back onto the main UI with a Delegate and Invoke().
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now