Recently, three-party State synchronization is required for projects, that is, PDM, CAD, and instant messaging tools, because these tools are not developed in one language. So how to do this is troublesome. The database-based monitoring is affirmative. How can we complete this monitoring? I first designed the monitoring system process and added the database judgment, but this will affect the performance of the software. So I thought of using system services to complete this transfer. Of course, it is just an assumption that it is still unknown whether it can be completed.
Open the C # project, find the windows Service Project, and create a new one. The design interface is the form of components. Go to the Code view: Wow, all written:
Three functions provided by the system: Main, Start, and Stop
Write a test function:
Private void WriteLog (string content)
{
String fPath = @ "d: \ Windows service \ log.txt ";
StreamWriter sWriter = null;
If (! File. Exists (fPath ))
File. Create (fPath );
Try
{
SWriter = new StreamWriter (fPath, true );
SWriter. WriteLine (content );
}
Catch (System. Exception err)
{
Throw new Exception ("service write error", err );
}
Finally
{
SWriter. Close ();
}
}
Add it to the start function to generate it. Run ----> the prompt cannot be executed directly. The Installer is required.
No way. I checked it online.
Originally, right-click the Component Interface and choose --> Add installation project. OK.
Set properties according to the online settings:
Set the properties of the serviceInstaller1 component:
1) ServiceName = My Sample Service
2) StartType = Automatic
Set the properties of the serviceProcessInstaller1 component
1) Account = LocalSystem
Run installutil.exe to register the instance. Enter the InstallUtil service name .exe at the. net command prompt.
Indicates that the registration is successful.
Find the service you just registered in service management.
BackD: \ WindowsService \ log.txt View
The "Service Startup..." information is added.
Haha, I tried the windows service for the first time. I don't know if I can play a proper role in reality.