« Service et csharp » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 38 : Ligne 38 :
# Right-click on {{boxx|MyService.cs}} → View Designer
# Right-click on {{boxx|MyService.cs}} → View Designer
# Right-click on the background → Add Installer
# Right-click on the background → Add Installer
# A new class {{boxx|ProjectInstaller}} has been added
# Right-click on {{boxx|ProjectInstaller.cs}} → View Designer
# Change the description to {{boxx|This service manages the ...}}

Version du 11 février 2022 à 22:07

Create the project

  1. File → New → Project
  2. Windows Service (.NET Framework)
  3. Rename Service1.cs to MyService.cs
  4. Double-click on MyService.cs to open the designer → Properties → ServiceName = MyService

Code

MyService.cs
public partial class MyService : ServiceBase
{
    public MyService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    { }

    protected override void OnStop()
    { }
}
Program.cs
static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
        new MyService()
    };
    ServiceBase.Run(ServicesToRun);
}

Installer

  1. Right-click on MyService.cs → View Designer
  2. Right-click on the background → Add Installer
  3. A new class ProjectInstaller has been added
  4. Right-click on ProjectInstaller.cs → View Designer
  5. Change the description to This service manages the ...