为什么我的服务不会重新启动并显示错误(附加)?

时间:2014-04-03 17:48:41

标签: c# visual-studio-2010 timer

在Windows Server 2003和Windows XP中,以自动(延迟)模式启动Windows服务的功能不可用,因此我需要使用代码进行模拟,但是当我启动服务时,这将显示一条消息需要等待cuz是代码中的一个Timer,这需要完成启动。但是现在我收到这个错误“本地计算机上的PcRegister服务服务已启动然后停止。如果没有工作要做,某些服务会自动停止,例如,性能日志和警报服务”...我需要帮助才能添加计时器。

class WinService : System.ServiceProcess.ServiceBase`
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new WinService() };
        ServiceBase.Run(ServicesToRun);
    }


    protected override void OnStart(string[] args)
    {
        InitializeComponent();
        PcRegisterService();
    }


    protected override void OnStop()
    {

    }


    public void InitializeComponent()
    {
        this.ServiceName = "WinService";
        RestartService("WinService", 600000);
    }


    public static void RestartService(string serviceName, int timeoutMilliseconds)
    {
        ServiceController service = new ServiceController(serviceName);

        try
        {
            int millisec1 = Environment.TickCount;
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            service.Stop();
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

            // count the rest of the timeout
            int millisec2 = Environment.TickCount;
            timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }

        catch
        {
            // ...
        }

    }


    public static void PcRegisterService()
    {
    }


    public static class PerformanceInfo
    {
        [DllImport("psapi.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size);

        [StructLayout(LayoutKind.Sequential)]
        public struct PerformanceInformation
        {
            public int Size;
            public IntPtr CommitTotal;
            public IntPtr CommitLimit;
            public IntPtr CommitPeak;
            public IntPtr PhysicalTotal;
            public IntPtr PhysicalAvailable;
            public IntPtr SystemCache;
            public IntPtr KernelTotal;
            public IntPtr KernelPaged;
            public IntPtr KernelNonPaged;
            public IntPtr PageSize;
            public int HandlesCount;
            public int ProcessCount;
            public int ThreadCount;
        }

        public static Int64 GetTotalMemoryInMiB()
        {
            PerformanceInformation pi = new PerformanceInformation();
            if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi)))
            {
                return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576));
            }
            else
            {
                return -1;
            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

因为 - 我会说 - 你的RestartServicve方法中有些东西(有例外)。代码毫无意义。完全没有。根本不需要重新启动服务。现在是服务如何运作。回到阅读文档。