Solved

Repeat a function every 2 mins

Posted on 2009-05-12
4
1,038 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
[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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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