tags:

views:

258

answers:

6

I have the following code, does this run an endless loop?
I am trying to schedule something every minute and the console application should run continuously until I close it.

class Program
{
  static int curMin;
  static int lastMinute = DateTime.Now.AddMinutes(-1).Minutes;

 static void Main(string[] args)
 {
   // Not sure about this line if it will run continuously every minute??
   System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimCallBack), null, 1000, 60000);

  Console.Read();
  timer.Dispose();
 }
   private static void TimCallBack(object o)
   {
      curMin = DateTime.Now.Minute;
      if (lastMinute < curMin)
      {
          // Do my work every minute
          lastMinute = curMin;
      }
    }

}
+10  A: 

KISS - or are you competing for the Rube Goldberg award? ;-)

static void Main(string[] args)
{
   while(true)
   {
     DoSomething();
     if(Console.KeyAvailable)
     {
        break;     
     }
     System.Threading.Thread.Sleep(60000);
   }
}
Sky Sanders
+2  A: 

I think your method should work assuming you don't press any keys on the console window. The answer above will definitely work but isn't the prettiest.

+1  A: 

As soon as your main() exits, all the other threads will be automatically closed, too.

Vlad
Does that mean the timer is designed properly?
Picflight
If you want a thread that doesn't close when the main thread exits, you'll have to create a `new Thread(myMethod)` and set its `thread.IsBackground = false`. It will then have its fair chance of exiting as well.
Patrick
+1  A: 

If it needs to run the whole time, might it be a better solution to create a service? Example here.

glenatron
+1  A: 

Why not add your application to the Windows Task scheduler and do just one "task" per startup of your console app (and don't bother thinking about scheduling yourself?)

And to answer your question: No your sample doesn't "Loop", it's event driven and will close on key press.

thijs
Its meant to test a few things, so adding to Widows task scheduler or creating a windows service is not an option.
Picflight
A: 

Using an event which times out for the stop might work, something like this:

class Program
{
    static TimeSpan _timeSpan = new TimeSpan(0, 0, 5);
    static ManualResetEvent _stop = new ManualResetEvent(false);

    static void Main(string[] args)
    {
        Console.TreatControlCAsInput = false;
        Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
        { 
            _stop.Set();
            e.Cancel = true;
        };

        while (!_stop.WaitOne(_timeSpan))
        {
            Console.WriteLine("Waiting...");
        }
        Console.WriteLine("Done.");
    }
}
avid
@avid, how does 'something like this' address the question in ANY way? I am sorry, I don't see it. The OP is obviously overcomplicating the situation and you have taken it even farther but have failed to demonstrate 'Scheduling something to run every minute'. sorry.. fail
Sky Sanders