How to run the method in a schedule??

  • Thread starter Thread starter yindevil
  • Start date Start date
Y

yindevil

HI~

I am coding a program that need to run a method in a schedule when
start a program, but i have no idea to make it(how to code it). Can
someone help me please?

My case:

The user can input many task(start time, stop time and action(call
which method)) at the same time, just like a table. When the current
time is equal the start time in each row of table, it will run a
method(for example: call a method to printout something in the
textbox...etc).

Is there any simple code for me to reference?

THX~
 
You need to use one of the Timer classes. Look at System.Timers.Timer, it
lets you execute something after a given interval.

-Oleg.
 
I found these code from MSDN

using System;
using System.Threading;

class TimerExampleState
{
public int counter = 0;
public Timer tmr;
}

class App
{
public static void Main()
{
TimerExampleState s = new TimerExampleState();

// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate = new TimerCallback(CheckStatus);

// Create a timer that waits one second, then invokes every second.
Timer timer = new Timer(timerDelegate, s,1000, 1000);

// Keep a handle to the timer, so it can be disposed.
s.tmr = timer;

// The main thread does nothing until the timer is disposed.
while(s.tmr != null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
}
// The following method is called by the timer's delegate.

static void CheckStatus(Object state)
{
TimerExampleState s =(TimerExampleState)state;
s.counter++;
Console.WriteLine("{0} Checking Status
{1}.",DateTime.Now.TimeOfDay, s.counter);
if(s.counter == 5)
{
// Shorten the period. Wait 10 seconds to restart the timer.
(s.tmr).Change(10000,100);
Console.WriteLine("changed...");
}
if(s.counter == 10)
{
Console.WriteLine("disposing of timer...");
s.tmr.Dispose();
s.tmr = null;
}
}
}

I think these code can solve my problem, but it can handle one task
only... how can i edit it to handle multi task? I don't know how to
edit it...
 
Back
Top