All pastes #606508 Raw Edit

Something

public text v1 · immutable
·published 2007-07-06 21:52 UTC
rendered paste body
using System;
using System.Diagnostics;
using System.Threading;

namespace Executive
{
    class Program
    {
        static void Main(string[] args)
        {
            Executive exec = new Executive();
            exec.TimeSpan = new TimeSpan(0, 0, 1);
            exec.Command = "TASKMAN.EXE";
            exec.Start += delegate(object sender, Process proc)
            {
                Console.WriteLine("Start! {0}", proc.Id);
            };
            exec.Stop += delegate(object sender, Process proc)
            {
                Console.WriteLine("Stop! {0}, {1}", proc.Id, proc.ExitCode);
            };
            exec.CreateTimer();
            Console.ReadLine();
        }
    }

    delegate void StartDelegate(object sender, Process process);
    delegate void StopDelegate(object sender, Process process);

    class Executive
    {
        private void TimerCallback(object state)
        {
            Process p = Process.Start(mProcessStartInfo);
            if(Start != null)
                Start(this, p);
            p.WaitForExit();
            if(Stop != null)
                Stop(this, p);
        }

        private TimeSpan mTimeSpan;
        private ProcessStartInfo mProcessStartInfo;

        public Executive(TimeSpan timeSpan, String cmd, String args)
        {
            mTimeSpan = timeSpan;

            mProcessStartInfo = new ProcessStartInfo();
            mProcessStartInfo.UseShellExecute = true;
            mProcessStartInfo.CreateNoWindow = true;
            mProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            mProcessStartInfo.FileName = cmd;
            mProcessStartInfo.Arguments = args;
        }

        public Executive(): this(TimeSpan.MinValue, String.Empty, String.Empty)
        {
        }

        public Timer CreateTimer()
        {
            Timer timer = new Timer(new TimerCallback(TimerCallback), this, mTimeSpan, mTimeSpan);
            return timer;
        }

        public TimeSpan TimeSpan
        {
            get
            {
                return mTimeSpan;
            }
            set
            {
                mTimeSpan = value;
            }
        }

        public String Command
        {
            get
            {
                return mProcessStartInfo.FileName;
            }
            set
            {
                mProcessStartInfo.FileName = value;
            }
        }

        public String Arguments
        {
            get
            {
                return mProcessStartInfo.Arguments;
            }
            set
            {
                mProcessStartInfo.Arguments = value;
            }
        }

        public event StartDelegate Start;
        public event StopDelegate Stop;
    }
}