V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
wangjincp
V2EX  ›  C#

C# 写的一个 Windows 服务,无法安装,一直报错!

  •  
  •   wangjincp · Sep 17, 2019 · 12315 views
    This topic created in 2417 days ago, the information mentioned may be changed or developed.
        public partial class MyService : ServiceBase
        {
            int i = 0;
            bool iswhile = true;
            public MyService()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                Task.Factory.StartNew(() =>
                {
                    while (iswhile)
                    {
                        LogHelper.Write("Run " + i);
                        Thread.Sleep(1000);
                    }
                });
            }
    
            protected override void OnStop()
            {
                iswhile = false;
                Thread.Sleep(1000);
                LogHelper.Write("End Run " + i);
            }
        }
    

    安装 bat 代码

    直接运行 bat 后出现情况 使用管理员运行 后直接出现情况

    安装环境和开发环境都是在本机进行。

    3 replies    2019-10-15 14:35:41 +08:00
    kiracyan
        1
    kiracyan  
       Sep 18, 2019
    建议使用 Topshelf,还方便调试 自身的服务容易遇到一些坑爹的权限问题
    kiracyan
        2
    kiracyan  
       Sep 18, 2019
    或者你把路径换一下 带空格的路径安装好像有问题,我以前碰到过
    xuanbg
        3
    xuanbg  
       Oct 15, 2019
    管理员模式进 CMD,cd 进入你 exe 所在的文件夹,然后执行 InstallUtil.exe WindowsServiceTest.exe 看报什么错
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2766 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 11:02 · PVG 19:02 · LAX 04:02 · JFK 07:02
    ♥ Do have faith in what you're doing.