Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# .NET中的Windows服务应用程序挂起;“开始”;开始时,它赢了';我也不会停止_C#_.net_Service_Windows Services_.net 4.5 - Fatal编程技术网

C# .NET中的Windows服务应用程序挂起;“开始”;开始时,它赢了';我也不会停止

C# .NET中的Windows服务应用程序挂起;“开始”;开始时,它赢了';我也不会停止,c#,.net,service,windows-services,.net-4.5,C#,.net,Service,Windows Services,.net 4.5,我正在写一个简单的windows服务来学习。这是我的项目结构: WindowsService.cs using System.ServiceProcess; using System.Diagnostics; namespace ServiceTest { class WindowsService : ServiceBase { /// <summary> /// Public Constructor for WindowsServ

我正在写一个简单的windows服务来学习。这是我的项目结构:

WindowsService.cs

using System.ServiceProcess;
using System.Diagnostics;

namespace ServiceTest
{
    class WindowsService : ServiceBase
    {
        /// <summary>
        /// Public Constructor for WindowsService.
        /// - Put all of your Initialization code here.
        /// </summary>
        EventLog serviceEventLog;
        public WindowsService()
        {
            // Configure service event logging
            serviceEventLog = new EventLog();
            this.AutoLog = false;
            if (!EventLog.SourceExists("MyApp"))
            {
                EventLog.CreateEventSource("MyApp", "ServiceTest");
            }
            serviceEventLog.Source = "MyApp";
            serviceEventLog.Log = "ServiceTest";

            // Configure service properties
            this.ServiceName = "My Windows Service";
            this.EventLog.Log = "ServiceTest";

            // List of events this service can handle
            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;
        }

        /// <summary>
        /// The Main Thread: This is where your Service is Run.
        /// </summary>
        static void Main()
        {
            ServiceBase.Run(new WindowsService());
        }

        /// <summary>
        /// Dispose of objects that need it here.
        /// </summary>
        /// <param name="disposing">Whether
        ///    or not disposing is going on.</param>
        protected override void Dispose(bool disposing)
        {
            // Custom code

            // Run parent code
            base.Dispose(disposing);
        }

        /// <summary>
        /// OnStart(): Put startup code here
        ///  - Start threads, get inital data, etc.
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            serviceEventLog.WriteEntry("Service Started");

            base.OnStart(args);
        }

        /// <summary>
        /// OnStop(): Put your stop code here
        /// - Stop threads, set final data, etc.
        /// </summary>
        protected override void OnStop()
        {
            serviceEventLog.WriteEntry("Service Stopped");

            base.OnStop();
        }

        /// <summary>
        /// OnPause: Put your pause code here
        /// - Pause working threads, etc.
        /// </summary>
        protected override void OnPause()
        {
            serviceEventLog.WriteEntry("Service Paused");

            base.OnPause();
        }

        /// <summary>
        /// OnContinue(): Put your continue code here
        /// - Un-pause working threads, etc.
        /// </summary>
        protected override void OnContinue()
        {
            serviceEventLog.WriteEntry("Service Resumed");

            base.OnContinue();
        }

        /// <summary>
        /// OnShutdown(): Called when the System is shutting down
        /// - Put code here when you need special handling
        ///   of code that deals with a system shutdown, such
        ///   as saving special data before shutdown.
        /// </summary>
        protected override void OnShutdown()
        {
            serviceEventLog.WriteEntry("Service Shutdown");

            base.OnShutdown();
        }
    }

}
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace ServiceTest
{
    [RunInstaller(true)]
    public class WindowsServiceInstaller : Installer
    {
        /// <summary>
        /// Public Constructor for WindowsServiceInstaller.
        /// - Put all of your Initialization code here.
        /// </summary>
        public WindowsServiceInstaller()
        {
            ServiceProcessInstaller serviceProcessInstaller =
                               new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            //# Service Account Information
            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            serviceProcessInstaller.Username = null;
            serviceProcessInstaller.Password = null;

            //# Service Information
            serviceInstaller.DisplayName = "My New C# Windows Service";
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            //# This must be identical to the WindowsService.ServiceBase name
            //# set in the constructor of WindowsService.cs
            serviceInstaller.ServiceName = "My Windows Service";

            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}
我以管理员身份运行了
install.bat
脚本,并安装了服务。当我单击“开始”时,它在启动时卡住:

如果我右键单击服务并单击
停止
,我会得到以下结果:


我在Windows 10上使用.NET Framework 4.5。有什么问题吗?

在安装了.net framework 4.5的Windows 7之后,我成功启动并停止了该服务。您可以尝试在OnStart()之上放置一个Debugger.Break()吗?只需将Visual Studio连接到服务进程,点击“暂停”并检查处理“启动”请求的线程的调用堆栈
@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX4%

echo Installing ServiceTest...
echo ---------------------------------------------------
InstallUtil /i "%~dp0ServiceTest.exe"
echo ---------------------------------------------------
echo Done.
@PAUSE