0
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# 的新手,这是我的第一个系统服务。请帮我解决这个问题

4

1 回答 1

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)towhile(!_stop)以便您可以在OnStop().

于 2012-04-07T12:44:54.567 回答