Click here to Skip to main content
13,048,011 members (112,322 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 29 Apr 2009

Execute a Program for n Seconds

, 29 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Start a program, execute for n seconds, then kill it


I have a script which runs many sub programs, each sub program fetches a web page, filters its contents and outputs frequently (the output are placed in stdout). It usually works fine, although sometimes it hangs. It is not good because one sub program hangs, the whole script hangs. When it hangs, I always start the Process Explorer to kill this sub program, then the script keeps running. I wanted the script to kill the dead sub program after 30 seconds automatically, so I wrote this program.

Sorry my English is bad.



Using the Code

This is a console application. This program's command line format is:

exec4.exe seconds commandline arguments        

For example, the following command will run "dir /s c:\", wait for 10 seconds and kill.

exec4.exe 10 cmd.exe /c dir /s c:\

You can get exec4.exe. Please download and extract, and compile:

csc exec4.cs

The source code is listed below:

using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
class Exec4
        public static extern int WinExec(string exeName, int operType);
        public static void Main(string []args)
                // args[0]           seconds
                // args[1..n]        commandline
                if (args.Length<1)
                        Console.WriteLine("\tExec4.exe Seconds Commandline");
                        // Check for arguments.
                        int Expires;
                                Expires = Convert.ToInt32(args[0]);
                                Expires = -1;
                        if (Expires<=0)
                                Console.Error.Write("Seconds argument error: ");
                        string cmdLine=args[1];
                        for (int i=2; i<args.Length; i++) cmdLine+=" "+args[i];
                        string AppName=args[1];
                        if (AppName.ToLower().EndsWith(".exe")) AppName=
					AppName.Substring(0, AppName.Length-4);
                        List<int> ProcessIds=new List<int>();
                        foreach (Process aProcess in Process.GetProcessesByName
                        int hr=WinExec(cmdLine,0);
                        if (hr<=31) 
                                // Execute failed.
                                Console.Error.Write("SubProgram executes abnormally: ");
                        DateTime dtStart=DateTime.Now;
                        // Get the process id of the sub program.
                        int SubProgramProcessId=-1;
                        foreach (Process aProcess in Process.GetProcessesByName
                                if (-1==ProcessIds.IndexOf(aProcess.Id)) 
                        if (-1==SubProgramProcessId)
                                // We need SubProgramProcessId to stop sub program.
                                Console.Error.WriteLine("Cannot stop SubProgram!");
                        while ((DateTime.Now - dtStart).TotalSeconds<Expires) 
                                //Program end normally.
                                int found=0;
                                foreach (Process aProcess in Process.GetProcessesByName
                                        if (SubProgramProcessId==aProcess.Id)
                                if (0==found) return;
                        //Program expired, kill it.
                        foreach (Process ExpiredProcess in Process.GetProcessesByName
                                if (SubProgramProcessId==ExpiredProcess.Id)

Points of Interest

This program uses WinExec Windows API to launch the sub program. It uses Process.GetProcessesByName() function to get related programs and kill the pending sub program as necessary.


This article describes an approach to start a sub program and stop it automatically. It doesn't kill the sub program's process tree. It introduces a method to kill the pending sub program without any warrant. I hope it is useful to you. Happy Labor's Day!


  • 30.04.09 - Original version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Taiwan Taiwan
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionUse a Timer? Pin
Pinx5-May-09 0:25
memberPinx5-May-09 0:25 
AnswerRe: Use a Timer? Pin
Linccg5-May-09 1:54
memberLinccg5-May-09 1:54 
GeneralRe: Use a Timer? Pin
Fiwel5-May-09 5:28
memberFiwel5-May-09 5:28 
GeneralRe: Use a Timer? Pin
Linccg5-May-09 7:26
memberLinccg5-May-09 7:26 
GeneralRe: Use a Timer? Pin
Fiwel5-May-09 7:35
memberFiwel5-May-09 7:35 
GeneralRe: Use a Timer? Pin
Linccg5-May-09 7:53
memberLinccg5-May-09 7:53 
GeneralRe: Use a Timer? Pin
Fiwel5-May-09 7:42
memberFiwel5-May-09 7:42 
GeneralRe: Use a Timer? Pin
Linccg5-May-09 9:06
memberLinccg5-May-09 9:06 
GeneralRe: Use a Timer? Pin
Fiwel5-May-09 9:18
memberFiwel5-May-09 9:18 
GeneralRe: Use a Timer? Pin
Linccg5-May-09 10:36
memberLinccg5-May-09 10:36 
Generalfetching web page Pin
TranQuangDao1-May-09 7:49
memberTranQuangDao1-May-09 7:49 
GeneralRe: fetching web page Pin
Linccg1-May-09 12:12
memberLinccg1-May-09 12:12 
GeneralRe: fetching web page Pin
TranQuangDao3-May-09 6:02
memberTranQuangDao3-May-09 6:02 
GeneralBug fixed Pin
Linccg29-Apr-09 16:16
memberLinccg29-Apr-09 16:16 
GeneralMy way Pin
PIEBALDconsult29-Apr-09 13:57
memberPIEBALDconsult29-Apr-09 13:57 
GeneralRe: My way Pin
Linccg29-Apr-09 15:26
memberLinccg29-Apr-09 15:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Apr 2009
Article Copyright 2009 by Linccg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid