Methods for creating multiple Windows services in one application and 1083 error resolution method
Add ' service install successful ' after self-boot function
--------------------------------------------------------------------------------------
1. Create a Windows Service project
New project, file---installed templates, Windows, Visual C #, select Windows Services in the right window
2. The system has set up a Service1.cs component for us, which we use when testing.
If you want a new piece, right-click Project, add New item--Windows services.
3. On Service1.cs, press F7 (right-click to view the code) to open the code page. Add the code for our test.
Note: The following several methods in the STR variable, in order for us to debug a moment when setting breakpoints with!
Using System;
Using System.Diagnostics;
Using System.ServiceProcess;
Namespace WindowsService1
{
public partial class Service1:servicebase
{
Public Service1 ()
{
InitializeComponent ();
Initservice ();
}
<summary>
Initializing Service parameters
</summary>
private void Initservice ()
{
Base. AutoLog = false;
Base. CanShutdown = true;
Base. CanStop = true;
Base. CanPauseAndContinue = true;
Base. ServiceName = "Service1"; This name is important, and setting inconsistencies will produce 1083 errors!
}
protected override void OnStart (string[] args)
{
String str = "Service open";
}
protected override void OnStop ()
{
String str = "Service stopped";
}
protected override void OnContinue ()
{
String str = "Service continues to run";
Base. OnContinue ();
}
protected override void OnPause ()
{
String str = "Service paused";
Base. OnPause ();
}
}
}
4. Setting up the "installer" for the service
4.1 Double-click "Service1.cs" to open the Service1.cs View Designer
4.2 Right-click anywhere in the View Designer and select "Add Installer"
4.3 Then the project will appear with a ProjectInstaller.cs component
(If you are adding "installer" for the first time)
4.4 Double-click ProjectInstaller.cs to open the ProjectInstaller View Designer
4.5 Locate the ServiceInstaller1 component, select and press the F4 key to set the component properties.
description= "test service 1";
Displayname= "Service1";
Servicename= "Service1";//This value must be the base set under the Windowsservice1.initservice () method. ServiceName attribute one to.
StartType is the service run type, which can be set according to your needs. (Manual: Manual start, AutoMatic for auto start)
4.6 Locate the ServiceProcessInstaller1 component, select and press the F4 key to set the component properties.
Account= "LocalSystem";//Set to another property prompts for a user name and password when the service is turned on
4.7 This completes all the preparation for a service, and the following is the installation and testing work.
5. Building Components
Right-click the project and select Build.
6. Installing components
6.1 Using the InstallUtil.exe tool to register Component Services, the file location is in a different version of the framework. I'm using 4.0, so I'm in a position.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe
6.2 Open the Build directory, if the build succeeds there will be a WindowsService1.exe file
6.3 New Add two Bat (batch file) under Build directory root for installation and uninstallation services
Create a new text file, open the file and add the following two lines of command
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe WindowsService1.exe
Pause
After saving, close the text file, and then rename it to "Install the service. bat" file, note to modify the suffix named. bat!
Create a new text file, open the file and add the following two lines of command
C:\windows\microsoft.net\framework\v4.0.30319\installutil.exe/u WindowsService1.exe
Pause
After saving, close the text file and rename it to "Uninstall service. Bat" file, note to modify the suffix named. bat!
6.4 "Install the service. bat" file, install the Windows service
7. After successful installation, we need to open the service manually because we just set Serviceinstaller1.starttype to Manual
Open Windows Service Manager, locate the service named Service1, and right-click Properties. Click on the "Start" button
8.windows Commissioning
8.1 Back to the development environment, select Debug, attach to process, tick "show all user Processes"
8.2 Find the WindowsService1.exe process (if no click on the Refresh button), select and click the "Attach to Process" button.
8.3 Select the Service1.cs file, and then press F7 to open the Code view
8.4 Adding a debug breakpoint on all str variables
8.5 Go back to Windows Service Manager, look for the Service1 service and select it, there is a "pause" button on the left. Click
At this point, the breakpoint under the OnPause method hits! OK, this is the purpose of the commissioning.
9. If you want to install multiple services at once, you need to follow 2~5 steps to add a few more services!
--------------------------------------------------------------------------------------
Problems and Solutions
Error encountered when starting Windows service after successful registration: 1083 Solution
1. Check whether the specified service is started in the Main () method
Servicebase[] ServicesToRun;
ServicesToRun = new servicebase[]
{
New Service1 (),
New Service2 ()//If you need to add this code to create multiple services
};
Servicebase.run (ServicesToRun);
2. Ensure that the ServiceName property of the ServiceInstaller1 component in the ProjectInstaller.cs View Designer
Consistent with the ServiceName property of the Service1.cs component
-----------------------------------------------------------------------------------------
Add the Self-boot feature after the service installation is successful
1. Open the ProjectInstaller.cs View Designer and check the ServiceInstaller1 component
2. Open the ServiceInstaller1 component's event window and double-click the Afterinstall event
3. Add the following code under the Serviceinstaller1_afterinstall event method
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController ();
Sc. ServiceName = "Expressdispatchingtrack"; The ServiceName property must be associated with the ServiceInstaller1 component in the ProjectInstaller.cs View Designer
Consistent
Sc. Start ();
Original address: http://write.blog.csdn.net/postedit/7542654
C # Create a Windows service (GO)