locked
Execute Every X Minutes RRS feed

  • Question

  • I have a console app that will FTP a file once the file is created in a specific directory.  I would like to change it up a bit, and have the app scan the directory every X minutes.  If there are files in the directory, FTP all of them and clear out the directory.  If the directory is empty, do nothing.  I would like this to repeat every 15 minutes while the app is running.  What is the best way to go about this?
    Friday, October 28, 2011 3:31 PM

Answers

  • Or Threading.Timer class.

    Simple example:

            static System.Threading.Timer t1;
            static void Main(string[] args)
            {
                t1 = new System.Threading.Timer(DoWork, null, 1000, 3000);            
                while (true) { } //this is only the code doesnt exits
            }
    
            private static void DoWork(object data)
            {
                Console.WriteLine("do checking...");
            }
    



    Mitja
    • Proposed as answer by sambeet Friday, October 28, 2011 4:48 PM
    • Marked as answer by Jay Mazz Friday, October 28, 2011 5:03 PM
    Friday, October 28, 2011 4:09 PM

All replies

  • Use the Timer class or Thread.Sleep().  If you use a timer, make sure that you check if you are still running from the previous tick so you aren't stepping on your own toes.  A shared boolean could do the trick.
    • Proposed as answer by sambeet Friday, October 28, 2011 4:48 PM
    Friday, October 28, 2011 3:53 PM
  • Or Threading.Timer class.

    Simple example:

            static System.Threading.Timer t1;
            static void Main(string[] args)
            {
                t1 = new System.Threading.Timer(DoWork, null, 1000, 3000);            
                while (true) { } //this is only the code doesnt exits
            }
    
            private static void DoWork(object data)
            {
                Console.WriteLine("do checking...");
            }
    



    Mitja
    • Proposed as answer by sambeet Friday, October 28, 2011 4:48 PM
    • Marked as answer by Jay Mazz Friday, October 28, 2011 5:03 PM
    Friday, October 28, 2011 4:09 PM