using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Collections;
namespace cloud_sync
{
public partial class cloud_sync : ServiceBase
{
public cloud_sync()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
while (true)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "C:\\ec2\\ec2.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = true;
proc.Start();
proc.WaitForExit();
string[] filePaths = Directory.GetFiles(@"c:\My cloud\VM Instances\");
foreach (string filePath in filePaths)
File.Delete(filePath);
try
{
StreamReader sr = new StreamReader("c:/ec2/temp.txt");
if (new FileInfo("c:/ec2/temp.txt").Length > 0)
{
string line, temp, temp1;
string[] content = new string[4];
line = GetWord(sr);
line = GetWord(sr);
string[] terms = line.ToLower().Trim().Split('\t');
Console.WriteLine(terms[1]);
Console.WriteLine(terms[5]);
Console.WriteLine(terms[9]);
temp1 = terms[1];
content[0] = "Instance_id :" + terms[1];
content[1] = "Status :" + terms[5];
content[2] = "Type :" + terms[9];
if (terms[5].Equals("terminated"))
{
line = GetWord(sr);
}
else
{
line = GetWord(sr);
line = GetWord(sr);
}
terms = line.ToLower().Trim().Split('\t');
Console.WriteLine(terms[4]);
content[3] = "Name :" + terms[4];
int i = terms[4].Length;
temp = "c:/My cloud/VM Instances/" + temp1 + " (" + terms[4] + ")";
temp = temp + ".cvm";
File.WriteAllLines(temp, content);
while ((line = GetWord(sr)) != null)
{
line = GetWord(sr);
terms = line.ToLower().Trim().Split('\t');
Console.WriteLine(terms[1]);
Console.WriteLine(terms[5]);
Console.WriteLine(terms[9]);
temp1 = terms[1];
content[0] = "Instance_id :" + terms[1];
content[1] = "Status :" + terms[5];
content[2] = "Type :" + terms[9];
if (terms[5].Equals("terminated"))
{
line = GetWord(sr);
}
else
{
line = GetWord(sr);
line = GetWord(sr);
}
terms = line.ToLower().Trim().Split('\t');
Console.WriteLine(terms[4]);
content[3] = "Name :" + terms[4];
i = terms[4].Length;
temp = "c:/My cloud/VM Instances/" + temp1 + " (" + terms[4] + ")";
temp = temp + ".cvm";
File.WriteAllLines(temp, content);
}
}
sr.Close();
sr.Dispose();
System.Threading.Thread.Sleep(50000);
File.Delete("c:/ec2/temp.txt");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
protected override void OnStop()
{
}
static string GetWord(StreamReader sr)
{
return sr.ReadLine();
}
}
}
I have written this system service but when I start this service, it show me error “Windows could not start the cloud_sync service on local computer. Error: 1053 the service did not respond to start and control request in timely fashion. “
and after pressing ok its status become “starting”. Please tell what is the problem with this.
I am greenhorn in c#, this is my first system service.
please help me out of this
You are receiving
Windows could not start the cloud_sync service on local computer. Error: 1053 the service did not respond to start and control request in timely fashionbecause yourOnStart()is not returning. You need to spin off another thread to execute your desired code.You should replace your
while(true)towhile(!_stop)so that you can gracefully end the loop withinOnStop().