wangjincp
V2EX  ›  C#

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

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