Solved

Class CountDown TimeSpan and Timer?

Posted on 2006-06-15
4
1,225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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