Solved

Repeat a function every 2 mins

Posted on 2009-05-12
4
1,032 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

8 Experts available now in Live!

Get 1:1 Help Now