Solved

Class CountDown TimeSpan and Timer?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 78
Windows Media Player 2 26
Hey Audio! 3 28
Graphics 2 27
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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