Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1047
  • Last Modified:

Repeat a function every 2 mins

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
mike_pro
Asked:
mike_pro
  • 2
2 Solutions
 
tcullerCommented:
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
 
tcullerCommented:
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
 
alb66Commented:
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now