Beruflich Dokumente
Kultur Dokumente
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
Step2. Run Visual Studio 2010 Command Prompt as administrator, navigate to the output folder of the sample project, and enter the following command to install the service. InstallUtil.exe CSWindowsService.exe The service is successfully installed if the process outputs:
1 of 7
14.4.2013 10:09
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
If you do not see this output, please look for the CSWindowsService.InstallLog file in the ouput folder, and investigate the cause of failure. Step3. Open Service Management Console (services.msc). You should be able to find "CSWindowsService Sample Service" in the service list.
Step4. Right-click the CSWindowsService service in Service Management Console and select Start to start the service. Open Event Viewer, and navigate to Windows Logs / Application. You should be able to see two events from CSWindowsService with event messages: "CSWindowsService in OnStart." and "Service started successfully."
2 of 7
14.4.2013 10:09
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
Right-click the service in Service Management Console and select Stop to stop the service. You will see two new events from CSWindowsService in Event Viewer / Windows Logs / Application with messages: "CSWindowsService in OnStop" and "Service stopped successfully".
Step5. To uninstall the service, enter the following command in Visual Studio 2010 Command Prompt running as administrator. InstallUtil /u CSWindowsService.exe If the service is successfully stopped and removed, you would see this output:
3 of 7
14.4.2013 10:09
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
14.4.2013 10:09
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
C# protected override void OnStart(string[] args) { // Log a service start message to the Application log. this.eventLog1.WriteEntry("CSWindowsService in OnStart.");
// Queue the main service function for execution in a worker thread. ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerThread)); }
SampleService.OnStop, which is executed when the service stops, calls EventLog.WriteEntry to log the service-stop information. Next, it sets the member varaible 'stopping' as true to indicate that the service is stopping and waits for the finish of the main service function that is signaled by the 'stoppedEvent' event object.
C# protected override void OnStop() { // Log a service stop message to the Application log. this.eventLog1.WriteEntry("CSWindowsService in OnStop.");
5 of 7
// Indicate that the service is stopping and wait for the finish
14.4.2013 10:09
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
// Indicate that the service is stopping and wait for the finish // of the main service function (ServiceWorkerThread). this.stopping = true; this.stoppedEvent.WaitOne(); }
SampleService.ServiceWorkerThread runs in a thread pool worker thread. It performs the main function of the service such as the communication with client applications through a named pipe. In order that the main function finishes gracefully when the service is about to stop, it should periodically check the 'stopping' varaible. When the function detects that the service is stopping, it cleans up the work and signal the 'stoppedEvent' event object.
C# private void ServiceWorkerThread(object state) { // Periodically check if the service is stopping. while (!this.stopping) { // Perform main service function here...
Thread.Sleep(2000); }
6 of 7
14.4.2013 10:09
http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f...
Step1. Add a new Other Project Types / Setup and Deployment Projects / Setup Project named CSWindowsServiceSetup. Step2. To add CSWindowsService.exe to the setup project, right-click CSWindowsServiceSetup, point to Add, and then click Project Output. Select CSWindowsService in the Project box and choose Primary Output from the list. A project item for the primary output of CSWindowsService is added to the setup project. Step3. By default, the target platform of the setup project is x86. If you want to build a setup that targets the x64 platform, click the setup project, and choose "x64" as the TargetPlatform in the Properties dialog. Step4. Now add a custom action to install the CSWindowsService.exe file. Right-click the setup project in Solution Explorer, point to View, and then click Custom Actions. In the Custom Actions editor, right-click the Custom Actions node and click Add Custom Action. Double-click the Application Folder in the list to open it, select Primary Output from CSWindowsService (Active), and click OK. The primary output is added to all four nodes of the custom actions Install, Commit, Rollback, and Uninstall. In Solution Explorer, right-click the CSWindowsServiceSetup project and click Build.
More Information
MSDN: Windows Service Applications
7 of 7
14.4.2013 10:09