Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- namespace ProductivityWatcher.Services {
- partial class pwa:ServiceBase {
- readonly PWService _service;
- public pwa() {
- InitializeComponent();
- _service = new PWService();
- }
- protected override void OnStart(string[] args) {
- try {
- bool flag = _service.Start(args);
- if(!flag) {
- LogError(message :"PWService failed to start, PWService->Start() returned 'false'",entryType :EventLogEntryType.FailureAudit);
- this.Stop();
- return;
- }
- Debug.WriteLine("PWService started");
- }
- catch(Exception error) {
- LogError(message :"PWService failed to start",error:error);
- this.Stop();
- }
- }
- protected override void OnStop() {
- try {
- if(!_service.IsStarted)
- return;
- bool flag = _service.Stop();
- if(!flag) {
- LogError(message :"PWService failed to stop, PWService->Stop() returned 'false'");
- return;
- }
- Debug.WriteLine("PWService stopped");
- }
- catch(Exception error) {
- LogError(message :"PWService failed to stop",error :error);
- }
- }
- void LogError(string message = null,Exception error = null,EventLogEntryType entryType = EventLogEntryType.Error) {
- string msg = message ?? "[NO MESSAGE] ";
- if(error != null) {
- msg += " - ";
- msg += error.Message;
- msg += Environment.NewLine;
- msg += error.ToString();
- }
- msg += " - Assembly Version: " + this.GetType().Assembly.GetName().Version.ToString();
- this.EventLog.WriteEntry(msg,EventLogEntryType.Error,PWService.EVENTLOG_ID);
- Debug.WriteLine(msg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment