心影(LM)

导航

debug 调试Windows service服务调试

Service1.cs

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WindowsService1
{
  public partial class Service1 : ServiceBase
  {
    public Service1()
    {
      InitializeComponent();
    }

    public void OnDebug()
    {
      string[] str = { "0" };
      OnStart(str);
    }

    protected override void OnStart(string[] args)
    {
      File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
    }

    protected override void OnStop()
    {
      File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
    }

  }
}
复制代码

Program.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsService1
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
#if DEBUG  
      Console.WriteLine("This is debug mode");
      Service1 myService = new Service1();
      myService.OnDebug();
#else
      ServiceBase[] ServicesToRun;
      ServicesToRun = new ServiceBase[]
      {
                new Service1()
      };
      ServiceBase.Run(ServicesToRun);
#endif

    }
  }
}
复制代码

 

posted on 2021-11-04 15:45  心影(LM)  阅读(142)  评论(0编辑  收藏  举报