Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Class CountDown TimeSpan and Timer?

Posted on 2006-06-15
4
Medium Priority
?
1,261 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 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

721 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