Solved

Repeat a function every 2 mins

Posted on 2009-05-12
4
1,036 Views
Last Modified: 2013-12-17
I have a function getColors() which i need to run every 2 mins (this function just queries a website for some data and updates some images). How can you get a function to do this?
0
Comment
Question by:mike_pro
  • 2
4 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24364265
Well, I don't know of a built-in function, but you can use a timer, instead. I'll give a sample code snippet.

        class TwoMinuteTimer : System.Timers.Timer
        {
            /// <summary>Initializes a new timer that fires its Elapsed event ever 120 seconds.</summary>
            /// <param name="myElapsedEvent">The method to call ever 2 minutes.</param>
            public TwoMinuteTimer(ElapsedEventHandler myElapsedEvent)
                : base(120000.0) // 120,000 milliseconds, aka 120 seconds/2 minutes for the interval.
            {
                this.Start();
                this.Elapsed += myElapsedEvent;
            }
        }

Open in new window

0
 
LVL 9

Accepted Solution

by:
tculler earned 250 total points
ID: 24364284
Sorry, hit the tab + enter by accident.

You would do the following to start retreiving your colors:

TwoMinuteTimer timer = new TwoMinuteTimer(MyEventHandler);

void MyEventHandler(System.Timers.ElapsedEventArgs elapArgs)
{
     // Get your color here. I recommend loading it into a buffer of some sort, like a List<Color>, or a Queue<Color>
}

Hope I helped, feel free to ask any other questions.
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 250 total points
ID: 24364287
You can add a timer to your form.

timer1 = new System.Timers.Timer();
timer1.Elapsed += new System.Timers.ElapsedEventHandler( timer1_Elapsed );

timer1.Interval = 120000;
timer1.Enabled = true;


private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
   ... do something
}
            
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

12 Experts available now in Live!

Get 1:1 Help Now