Hi

I am trying to create a service in c#, if i click start in service it has to execute the run.bat file. if i click stop in service it has to execute the stop.bat file

can any one please help me to fix the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace SmartSchedulerService_Test
{
    public partial class SmartSchedulerService : ServiceBase
    {
        public SmartSchedulerService()
        {
            InitializeComponent();
        }
        TestWriteFile process;
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            base.OnStart(args);
            process = new TestWriteFile();
            process.StartProcess();
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            base.OnStop();
            process.StopProcess();
        }

        public class TestWriteFile
        {
            public static bool Shutdown = false;
            Thread ServiceThread;

            public static void WriteCurrentTimewithService()
            {
                if (Shutdown == false)
                {


                    String str = AppDomain.CurrentDomain.BaseDirectory + "\run.bat";
                    Process process = new Process();
                    process.StartInfo.FileName = str;
                    process.Start();

                }               
                else
                {
                    String str = AppDomain.CurrentDomain.BaseDirectory + "\stop.bat";
                    Process process = new Process();
                    process.StartInfo.FileName = str;
                    process.Start();
                }
            }

            public void StartProcess()
            {
                ServiceThread = new Thread(new ThreadStart(WriteCurrentTimewithService));
                ServiceThread.Start();
            }

            public void StopProcess()
            {
                Shutdown = true;
            }

        }

    }
}
This question has already been answered. Start a new discussion instead.