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();
}
}
}
我已编写此系统服务,但是当我启动此服务时,它会显示错误“Windows无法在本地计算机上启动cloud_sync服务。错误:1053服务未及时响应启动和控制请求时尚。“ 按下确定后,其状态变为“开始”。请告诉我这是什么问题。 我是c#中的新手,这是我的第一个系统服务。 请帮我解决这个问题
答案 0 :(得分:2)
您收到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
因为OnStart()
未返回。您需要分离另一个线程来执行您想要的代码。
private Task _serviceTask;
private bool _stop;
protected override void OnStart(string[] args)
{
_serviceTask = Task.Factory.StartNew(SomeTask)
}
// Rename the method from SomeTask to something that makes more sense
private static void SomeTask()
{
// Move your code here
}
您应该将while(true)
替换为while(!_stop)
,以便在OnStop()
内优雅地结束循环。