How to execute batch files at a specific time in folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rosae619
    New Member
    • Sep 2010
    • 1

    #1

    How to execute batch files at a specific time in folder

    Hi all,

    Here I want to execute batch files for certain time

    located in a folder.

    I want to execute in such fashion:

    1) First batch (it executes for certain time, get terminated)

    2) second batch (after termination of first batch,second batch file should start executing..)

    3)third batch(after termination of second batch third file should start executing..) and so on.

    Problem is that, before termination of second batch file,third file get started and so on.


    I have attached my code.plz help

    TimerElapsed event also fires more than once for each event.How to make it fire only once for each event?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Timers;
    using System.Diagnostics;
    using System.IO;
    
    namespace Testing_2
    {
        class Program
        {
            bool blnTimeElapsed = false;
    
            void TimerElapsed(object sender, ElapsedEventArgs e)
            {
                this.blnTimeElapsed = true;
                Console.WriteLine("event fire");
            }
    
            static void Main(string[] args)
            {
                Program run = new Program();
                run.Run();
            }
            void Run()
            {
                string[] files = Directory.GetFiles(@"C:\Files\");
                foreach (string file in files)
                {
                    string filename = file;
                    Process proc = new Process();
                    proc.StartInfo.FileName = filename;
                    Timer time = new Timer();
                    time.Interval = 10000;
                    time.Elapsed += new ElapsedEventHandler  (TimerElapsed);
                     ProcessStartInfo(filename);
                    proc.Start();
                    time.Enabled = true;
                    Console.WriteLine(blnTimeElapsed);
                    while (!proc.HasExited)
                    {
                        if (blnTimeElapsed)
                        {
    
                           Console.WriteLine("force fully closed");
                            proc.CloseMainWindow();
                            break;
                         }
                    }
                }
             }
        }
    }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Is the purpose of this timer to serve as a timeout? Like, if your process takes too long to execute, it will fire the event and abort processing?

    Your event looks fine... 10 seconds? I don't know why it fires twice in quick succession. You could disable your event in the firing code and that would make sure it gets fired only the one time.

    I'm not seeing a second event firing when I try a similar thing...

    Code:
            private static int m_runCount = 0;
    
            static void Main(string[] args)
            {
                Timer t = new Timer();
                t.Interval = 1000;
                t.Elapsed += new ElapsedEventHandler(t_Elapsed);
                t.Start();
    
                while (m_runCount < 10)
                {
                }
    
                Console.WriteLine();
                Console.WriteLine("Execution finished");
            }
    
            static void t_Elapsed(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("Event fired... executed iteration " + m_runCount.ToString());
    
                m_runCount++;
            }
    Maybe the extra fire is related to why it's starting the second batch too early?

    Comment

    Working...