Solved

Class CountDown TimeSpan and Timer?

Posted on 2006-06-15
4
1,189 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

11 Experts available now in Live!

Get 1:1 Help Now