Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public partial class Service1 : ServiceBase
    {
        private Timer timer1 = null;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new Timer();
            this.timer1.Interval = 60000; //60 sec
            this.timer1.Elapsed +=new System.Timers.ElapsedEventHandler(this.timer1_Tick);
            timer1.Enabled=true;
            Library.WriteErrorLog("test windows service started");
            
        }

        private void timer1_Tick(object sender, ElapsedEventArgs e)
        {
            

            Process proc = null;
            try
            {
                string batrun = string.Format("cmd.exe","/c" + @"C:\Abhay\batfile");
                proc = new Process();
                proc.StartInfo.WorkingDirectory = batrun;
                proc.StartInfo.FileName = "webupknvp";
                proc.StartInfo.CreateNoWindow = false;
                proc.Start();
                proc.WaitForExit();
                
                
              

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            Library.WriteErrorLog("timer ticked and batch file executed");

        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            Library.WriteErrorLog("Test Service ended");
        }


What I have tried:

We are stuck here still searching a possible way!!.
Posted
Updated 9-Mar-17 20:38pm

1 solution

After
C#
p.WaitForExit();
Don't forget to check its return value to make sure it actually was successful:
C#
if(p.ExitCode == 0) { // Or whatever return code you're expecting
    //...
}
If you are still having problems, move the code to its own function and run manually:
C#
private int RunProcess(string workDir, string appName, string args, bool hide = false)
{
	string batrun = string.Format("cmd.exe","/c" + @"C:\Abhay\batfile");
	proc = new Process();
	proc.StartInfo.WorkingDirectory = workDir;
	proc.StartInfo.FileName = appName;
	proc.StartInfo.Arguments = args
	proc.StartInfo.CreateNoWindow = hide;
	
	proc.Start();
	proc.WaitForExit();
	
	return p.ExitCode
}
Then testing and using from your timer event becomes very easy:
C#
var result = RunProcess("c:\", "file.Bat", "", false);
if (result == 0)
{
	// success
}
else
{
	// failed ErrorLevel / app ExitCode
}
 
Share this answer
 
v2
Comments
r.abhaysinghania 10-Mar-17 4:56am    
Thank you for the help!!!. It Worked @Graeme_Grant
Graeme_Grant 10-Mar-17 4:58am    
you are most welcome. :)
r.abhaysinghania 10-Mar-17 5:06am    
can i ask one more question?
is it possible to run multiple bat files with reusing the above code instead of making more methods? or coping and pasting the code?
Graeme_Grant 10-Mar-17 5:08am    
There is no reason why not.
r.abhaysinghania 10-Mar-17 5:10am    
how is it possible i tried with coping and pasting the code but the load increased. is there any way to reuse the above code for input of multiple batch file?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900