Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的服务不重新启动并显示错误(已连接)?_C#_Visual Studio 2010_Timer - Fatal编程技术网

C# 为什么我的服务不重新启动并显示错误(已连接)?

C# 为什么我的服务不重新启动并显示错误(已连接)?,c#,visual-studio-2010,timer,C#,Visual Studio 2010,Timer,在Windows Server 2003和Windows XP中,以自动(延迟)模式启动Windows服务的功能不可用,因此我需要使用代码进行模拟,但当我启动服务时,会显示一条消息,说明我需要等待,因为代码中有计时器,这需要完成才能启动。但现在我遇到了这个错误“本地计算机上的PcRegister服务启动,然后停止。如果没有工作要做,某些服务会自动停止,例如,性能日志和警报服务”。。。请帮我添加计时器 class WinService : System.ServiceProcess.Service

在Windows Server 2003和Windows XP中,以自动(延迟)模式启动Windows服务的功能不可用,因此我需要使用代码进行模拟,但当我启动服务时,会显示一条消息,说明我需要等待,因为代码中有计时器,这需要完成才能启动。但现在我遇到了这个错误“本地计算机上的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;
            }

        }
    }
}

因为——我可以说——在RestartServicve方法中有一些东西是你吹的(除了一个例外)。这个代码毫无意义。一点也没有。根本不需要重新启动服务。这就是服务的工作方式。回到阅读文档。

对不起,我是编程界的新手,在这项工作中,所有人都告诉我谷歌是我最好的朋友。。。服务中的代码太长了,这是我不包括它的原因。。。我需要等待服务在最后开始,因为与另一台服务器建立网络连接,因此如果没有网络连接,信息就无法发送。世界上到处都是知识渊博的人。忘记谷歌吧。阅读文档,学习语言。买一两本好书。不要寻找单一项目的解决方案,学习基础知识。你推荐哪本书?C#让dummies开始(整个dummies系列做得很好),然后是整个相关的.nET文档。