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;
            }

        }

    }
}

with the code you currently have, what happens?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.