From 7c19f8432cdd6a94397a43e2aa1859a012b52a1e Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 26 Apr 2024 09:52:44 -0700 Subject: [PATCH] fix build warnings --- .../MyNewService/csharp/MyNewService.cs | 278 ++++++++++-------- .../csharp/Program-add-parameter.cs | 21 +- .../MyNewService/csharp/Project.csproj | 13 + ...e-application-in-the-component-designer.md | 34 +-- 4 files changed, 183 insertions(+), 163 deletions(-) create mode 100644 docs/framework/windows-services/snippets/MyNewService/csharp/Project.csproj diff --git a/docs/framework/windows-services/snippets/MyNewService/csharp/MyNewService.cs b/docs/framework/windows-services/snippets/MyNewService/csharp/MyNewService.cs index d9c6c10b2f33e..6624025537f2a 100644 --- a/docs/framework/windows-services/snippets/MyNewService/csharp/MyNewService.cs +++ b/docs/framework/windows-services/snippets/MyNewService/csharp/MyNewService.cs @@ -1,86 +1,114 @@ //******************************************************************* -using System; -using System.Collections.Generic; -using System.Text; +using System.Diagnostics; using System.ServiceProcess; -public class Service1: System.ServiceProcess.ServiceBase +public class Service1 : ServiceBase { - void MainOriginal() - { - // - System.ServiceProcess.ServiceBase[] ServicesToRun; - ServicesToRun = new System.ServiceProcess.ServiceBase[] - { new Service1() }; - System.ServiceProcess.ServiceBase.Run(ServicesToRun); - // - } + static void MainOriginal() + { + // + ServiceBase[] ServicesToRun; + ServicesToRun = new ServiceBase[] + { new Service1() }; + Run(ServicesToRun); + // + } } -public class MyNewService: System.ServiceProcess.ServiceBase +public class MyNewService : ServiceBase { - // - static void Main() - { - System.ServiceProcess.ServiceBase[] ServicesToRun; - // Change the following line to match. - ServicesToRun = new System.ServiceProcess.ServiceBase[] - { new MyNewService() }; - System.ServiceProcess.ServiceBase.Run(ServicesToRun); - } - // - - // - private System.ComponentModel.IContainer components; - private System.Diagnostics.EventLog eventLog1; - // - - [System.Diagnostics.DebuggerStepThrough()] - void InitializeComponent() - { - components = new System.ComponentModel.Container(); - this.ServiceName = "MyNewService"; - } - - // - public MyNewService() - { + // + static void Main() + { + ServiceBase[] ServicesToRun; + // Change the following line to match. + ServicesToRun = new ServiceBase[] + { new MyNewService() }; + ServiceBase.Run(ServicesToRun); + } + // + + // + private System.ComponentModel.IContainer _components; + private readonly EventLog _eventLog1; + // + + [DebuggerStepThrough()] + void InitializeComponent() + { + _components = new System.ComponentModel.Container(); + ServiceName = "MyNewService"; + } + + // + public MyNewService() + { InitializeComponent(); - eventLog1 = new EventLog(); + _eventLog1 = new EventLog(); if (!EventLog.SourceExists("MySource")) { EventLog.CreateEventSource("MySource", "MyNewLog"); } - eventLog1.Source = "MySource"; - eventLog1.Log = "MyNewLog"; - } - // - - // - protected override void OnStart(string[] args) - { - eventLog1.WriteEntry("In OnStart."); - } - // - - // - protected override void OnStop() - { - eventLog1.WriteEntry("In OnStop."); - } - // - - // - protected override void OnContinue() - { - eventLog1.WriteEntry("In OnContinue."); - } - // + _eventLog1.Source = "MySource"; + _eventLog1.Log = "MyNewLog"; + } + // + + // + public MyNewService(string[] args) + { + InitializeComponent(); + + string eventSourceName = "MySource"; + string logName = "MyNewLog"; + + if (args.Length > 0) + { + eventSourceName = args[0]; + } + + if (args.Length > 1) + { + logName = args[1]; + } + + _eventLog1 = new EventLog(); + + if (!EventLog.SourceExists(eventSourceName)) + { + EventLog.CreateEventSource(eventSourceName, logName); + } + + _eventLog1.Source = eventSourceName; + _eventLog1.Log = logName; + } + // + + // + protected override void OnStart(string[] args) + { + _eventLog1.WriteEntry("In OnStart."); + } + // + + // + protected override void OnStop() + { + _eventLog1.WriteEntry("In OnStop."); + } + // + + // + protected override void OnContinue() + { + _eventLog1.WriteEntry("In OnContinue."); + } + // } //******************************************************************* // -public class UserService1 : System.ServiceProcess.ServiceBase +public class UserService1 : ServiceBase { } // @@ -88,66 +116,66 @@ public class UserService1 : System.ServiceProcess.ServiceBase //******************************************************************* namespace WrapUserService1 { - public class UserService1:System.ServiceProcess.ServiceBase - { - // - public UserService1() - { - this.ServiceName = "MyService2"; - this.CanStop = true; - this.CanPauseAndContinue = true; - this.AutoLog = true; - } - // - - // - public static void Main() - { - System.ServiceProcess.ServiceBase.Run(new UserService1()); - } - // - - // - protected override void OnStart(string[] args) - { - // Insert code here to define processing. - } - // - } + public class UserService1 : ServiceBase + { + // + public UserService1() + { + ServiceName = "MyService2"; + CanStop = true; + CanPauseAndContinue = true; + AutoLog = true; + } + // + + // + public static void Main() + { + ServiceBase.Run(new UserService1()); + } + // + + // + protected override void OnStart(string[] args) + { + // Insert code here to define processing. + } + // + } } //******************************************************************* -class UserService2:System.ServiceProcess.ServiceBase +class UserService2 : ServiceBase { - System.Diagnostics.EventLog eventLog1; - - // - public UserService2() - { - eventLog1 = new System.Diagnostics.EventLog(); - // Turn off autologging - - // - this.AutoLog = false; - // - // create an event source, specifying the name of a log that - // does not currently exist to create a new, custom log - if (!System.Diagnostics.EventLog.SourceExists("MySource")) - { - System.Diagnostics.EventLog.CreateEventSource( - "MySource","MyLog"); - } - // configure the event log instance to use this source name - eventLog1.Source = "MySource"; - eventLog1.Log = "MyLog"; - } - // - - // - protected override void OnStart(string[] args) - { - // write an entry to the log - eventLog1.WriteEntry("In OnStart."); - } - // + readonly EventLog _eventLog1; + + // + public UserService2() + { + _eventLog1 = new EventLog(); + // Turn off autologging + + // + AutoLog = false; + // + // create an event source, specifying the name of a log that + // does not currently exist to create a new, custom log + if (!EventLog.SourceExists("MySource")) + { + EventLog.CreateEventSource( + "MySource", "MyLog"); + } + // configure the event log instance to use this source name + _eventLog1.Source = "MySource"; + _eventLog1.Log = "MyLog"; + } + // + + // + protected override void OnStart(string[] args) + { + // write an entry to the log + _eventLog1.WriteEntry("In OnStart."); + } + // } diff --git a/docs/framework/windows-services/snippets/MyNewService/csharp/Program-add-parameter.cs b/docs/framework/windows-services/snippets/MyNewService/csharp/Program-add-parameter.cs index 49a5d141ffaca..0339428cee8e3 100644 --- a/docs/framework/windows-services/snippets/MyNewService/csharp/Program-add-parameter.cs +++ b/docs/framework/windows-services/snippets/MyNewService/csharp/Program-add-parameter.cs @@ -1,9 +1,16 @@ -static void Main(string[] args) +using System.ServiceProcess; + +public class Class1 { - ServiceBase[] ServicesToRun; - ServicesToRun = new ServiceBase[] + // + static void Main(string[] args) { - new MyNewService(args) - }; - ServiceBase.Run(ServicesToRun); -} \ No newline at end of file + ServiceBase[] ServicesToRun; + ServicesToRun = new ServiceBase[] + { + new MyNewService(args) + }; + ServiceBase.Run(ServicesToRun); + } + // +} diff --git a/docs/framework/windows-services/snippets/MyNewService/csharp/Project.csproj b/docs/framework/windows-services/snippets/MyNewService/csharp/Project.csproj new file mode 100644 index 0000000000000..b21a7960acc42 --- /dev/null +++ b/docs/framework/windows-services/snippets/MyNewService/csharp/Project.csproj @@ -0,0 +1,13 @@ + + + + Exe + net481 + Class1 + + + + + + + diff --git a/docs/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer.md b/docs/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer.md index 3444731127d31..560cd888fa74f 100644 --- a/docs/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer.md +++ b/docs/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer.md @@ -362,35 +362,7 @@ Each Windows service has a registry entry under the **HKEY_LOCAL_MACHINE\SYSTEM\ 1. In **MyNewService.cs**, or **MyNewService.vb**, change the `MyNewService` constructor to accept and process an input parameter: - ```csharp - public MyNewService(string[] args) - { - InitializeComponent(); - - string eventSourceName = "MySource"; - string logName = "MyNewLog"; - - if (args.Length > 0) - { - eventSourceName = args[0]; - } - - if (args.Length > 1) - { - logName = args[1]; - } - - eventLog1 = new EventLog(); - - if (!EventLog.SourceExists(eventSourceName)) - { - EventLog.CreateEventSource(eventSourceName, logName); - } - - eventLog1.Source = eventSourceName; - eventLog1.Log = logName; - } - ``` + [!code-csharp[VbRadconService#2](./snippets/MyNewService/csharp/MyNewService.cs#ContructorWithArgs)] ```vb Public Sub New(ByVal cmdArgs() As String) @@ -416,7 +388,7 @@ Each Windows service has a registry entry under the **HKEY_LOCAL_MACHINE\SYSTEM\ 1. Select **Program.cs**, or **MyNewService.Designer.vb**, then choose **View Code** from the shortcut menu. In the `Main` method, change the code to add an input parameter and pass it to the service constructor: - [!code-csharp[VbRadconService](./snippets/MyNewService/csharp/Program-add-parameter.cs?highlight=1,6)] + [!code-csharp[VbRadconService](./snippets/MyNewService/csharp/Program-add-parameter.cs#1?highlight=6,11)] [!code-vb[VbRadconService](../../../samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbRadconService/VB/MyNewService.Designer-add-parameter.vb?highlight=1-2)] 1. To specify the command-line arguments, add the following code to the `ProjectInstaller` class in **ProjectInstaller.cs**, or **ProjectInstaller.vb**: @@ -481,7 +453,7 @@ For more information, see [How to: Install and uninstall services](how-to-instal You should see your service listed in **Services**, displayed alphabetically by the display name that you set for it. - ![MyNewService in the Services window.](./media/windowsservices-serviceswindow.png) + ![MyNewService in the Services window.](./media/windowsservices-serviceswindow.PNG) 2. To start the service, choose **Start** from the service's shortcut menu.