I have a windows service that run a batch process.When I try to start this services it is blocking in starting.
public class Heartbeat
{
Process _process = new Process();
public Heartbeat()
{
string result = String.Empty;
string path = @"C:\Data\script.bat";
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = path;
_process.StartInfo = startinfo;
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.RedirectStandardOutput = true;
_process.StartInfo.CreateNoWindow = true;
_process.Start();
while (!_process.StandardOutput.EndOfStream)
{
result = _process.StandardOutput.ReadLine();
result += "mama";
File.WriteAllText(@"C:\Data\Demos.txt",result);
}
}
public void Start()
{
_process.Start();
}
public void Stop()
{
_process.Kill();
}
}
static void Main(string[] args)
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var exitCode = HostFactory.Run(x=>
{
x.Service<Heartbeat>(s =>
{
s.ConstructUsing(heartbeat => new Heartbeat());
s.WhenStarted(heartbeat => heartbeat.Start());
s.WhenStopped(heartbeat => heartbeat.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("HeartbeatService");
x.SetDisplayName("Heartbeat Service");
x.SetDescription("This is the sample heartbeat service used in a YouTube demo.");
});
int exitCodeValue=(int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
Environment.ExitCode = exitCodeValue;
}
After a while I get this error.I think this is error is when I try to start this process.I also try to fix this with the help of this link: https://ugetfix.com/ask/fix-error-1053-the-service-did-not-respond-to-the-start-or-control-request-in-a-timely-fashion/#:~:text=version%20of%20Reimage%20.-,Error%201053%3A%20The%20service%20did%20not%20respond%20to%20the%20start,not%20being%20able%20to%20launch.
My batch file is :
@echo off
set message=Hello World
echo %message%
pause