andrew4582

Windows Service - Logic (Basic)

Aug 26th, 2010
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Reflection;
  10.  
  11. namespace ProductivityWatcher.Services {
  12.     public class PWService:IDisposable {
  13.  
  14.         public const int EVENTLOG_ID = 849;
  15.         public const string EVENTLOG_SOURCE = "PWService";
  16.  
  17.         bool _started;
  18.  
  19.         public bool IsStarted {
  20.             get {
  21.                 return _started;
  22.             }
  23.         }
  24.         public bool Start(string[] args) {
  25.  
  26.             try {
  27.                 if(_started) {
  28.                     LogError("Can not Start,Service already started");
  29.                     return false;
  30.                 }
  31.                 _started = true;
  32.  
  33.  
  34.                 return true;
  35.             }
  36.             catch(Exception error) {
  37.                 _started = false;
  38.                 LogError(error :error);
  39.                 return false;
  40.             }
  41.         }
  42.  
  43.         public bool Stop() {
  44.  
  45.             try {
  46.                 if(!_started) {
  47.                     LogError("Can not Stop, Service is not started");
  48.                     return false;
  49.                 }
  50.  
  51.  
  52.                 _started = false;
  53.                 return true;
  54.  
  55.             }
  56.             catch(Exception error) {
  57.                 LogError(error :error);
  58.                 return false;
  59.             }
  60.         }
  61.  
  62.         void TraceInfo(object value) {
  63.             Trace.TraceInformation(value == null ? "" : value.ToString());
  64.         }
  65.         void LogError(string message = null,Exception error = null,EventLogEntryType entryType = EventLogEntryType.Error) {
  66.  
  67.             string msg = message ?? "[NO MESSAGE] ";
  68.             if(error != null) {
  69.                 msg += " - ";
  70.                 msg += error.Message;
  71.                 msg += Environment.NewLine;
  72.                 msg += error.ToString();
  73.             }
  74.             msg += " - Assembly Version: " + this.GetType().Assembly.GetName().Version.ToString();
  75.             EventLog.WriteEntry(PWService.EVENTLOG_SOURCE,msg,EventLogEntryType.Error,PWService.EVENTLOG_ID);
  76.  
  77.             Trace.TraceError(msg);
  78.             Debug.WriteLine(msg);
  79.         }
  80.  
  81.         #region IDisposable Pattern
  82.         protected bool IsDisposed { get; private set; }
  83.  
  84.         public virtual void Dispose() {
  85.             if(IsDisposed)
  86.                 throw new ObjectDisposedException(this.GetType().Name);
  87.             try {
  88.                 this.Dispose(true);
  89.             }
  90.             finally {
  91.                 GC.SuppressFinalize(this);
  92.             }
  93.         }
  94.         protected virtual void Dispose(bool disposing) {
  95.             try {
  96.                 if(!IsDisposed) {
  97.                     if(disposing) {
  98.                         //Do object cleanup here
  99.  
  100.                     }
  101.                 }
  102.             }
  103.             finally {
  104.                 this.IsDisposed = true;
  105.             }
  106.         }
  107.         #endregion
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment