469,591 Members | 1,839 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

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?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Timers;
  5. using System.Diagnostics;
  6. using System.IO;
  7.  
  8. namespace Testing_2
  9. {
  10.     class Program
  11.     {
  12.         bool blnTimeElapsed = false;
  13.  
  14.         void TimerElapsed(object sender, ElapsedEventArgs e)
  15.         {
  16.             this.blnTimeElapsed = true;
  17.             Console.WriteLine("event fire");
  18.         }
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             Program run = new Program();
  23.             run.Run();
  24.         }
  25.         void Run()
  26.         {
  27.             string[] files = Directory.GetFiles(@"C:\Files\");
  28.             foreach (string file in files)
  29.             {
  30.                 string filename = file;
  31.                 Process proc = new Process();
  32.                 proc.StartInfo.FileName = filename;
  33.                 Timer time = new Timer();
  34.                 time.Interval = 10000;
  35.                 time.Elapsed += new ElapsedEventHandler  (TimerElapsed);
  36.                  ProcessStartInfo(filename);
  37.                 proc.Start();
  38.                 time.Enabled = true;
  39.                 Console.WriteLine(blnTimeElapsed);
  40.                 while (!proc.HasExited)
  41.                 {
  42.                     if (blnTimeElapsed)
  43.                     {
  44.  
  45.                        Console.WriteLine("force fully closed");
  46.                         proc.CloseMainWindow();
  47.                         break;
  48.                      }
  49.                 }
  50.             }
  51.          }
  52.     }
  53. }
  54.  
  55.  
Sep 23 '10 #1
1 2683
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1.         private static int m_runCount = 0;
  2.  
  3.         static void Main(string[] args)
  4.         {
  5.             Timer t = new Timer();
  6.             t.Interval = 1000;
  7.             t.Elapsed += new ElapsedEventHandler(t_Elapsed);
  8.             t.Start();
  9.  
  10.             while (m_runCount < 10)
  11.             {
  12.             }
  13.  
  14.             Console.WriteLine();
  15.             Console.WriteLine("Execution finished");
  16.         }
  17.  
  18.         static void t_Elapsed(object sender, ElapsedEventArgs e)
  19.         {
  20.             Console.WriteLine("Event fired... executed iteration " + m_runCount.ToString());
  21.  
  22.             m_runCount++;
  23.         }
Maybe the extra fire is related to why it's starting the second batch too early?
Sep 23 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by erniedude | last post: by
6 posts views Thread by Charles Neitzel | last post: by
3 posts views Thread by Danel | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.