Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Reflection;
- namespace ProductivityWatcher.Services {
- public class PWService:IDisposable {
- public const int EVENTLOG_ID = 849;
- public const string EVENTLOG_SOURCE = "PWService";
- bool _started;
- public bool IsStarted {
- get {
- return _started;
- }
- }
- public bool Start(string[] args) {
- try {
- if(_started) {
- LogError("Can not Start,Service already started");
- return false;
- }
- _started = true;
- return true;
- }
- catch(Exception error) {
- _started = false;
- LogError(error :error);
- return false;
- }
- }
- public bool Stop() {
- try {
- if(!_started) {
- LogError("Can not Stop, Service is not started");
- return false;
- }
- _started = false;
- return true;
- }
- catch(Exception error) {
- LogError(error :error);
- return false;
- }
- }
- void TraceInfo(object value) {
- Trace.TraceInformation(value == null ? "" : value.ToString());
- }
- 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();
- EventLog.WriteEntry(PWService.EVENTLOG_SOURCE,msg,EventLogEntryType.Error,PWService.EVENTLOG_ID);
- Trace.TraceError(msg);
- Debug.WriteLine(msg);
- }
- #region IDisposable Pattern
- protected bool IsDisposed { get; private set; }
- public virtual void Dispose() {
- if(IsDisposed)
- throw new ObjectDisposedException(this.GetType().Name);
- try {
- this.Dispose(true);
- }
- finally {
- GC.SuppressFinalize(this);
- }
- }
- protected virtual void Dispose(bool disposing) {
- try {
- if(!IsDisposed) {
- if(disposing) {
- //Do object cleanup here
- }
- }
- }
- finally {
- this.IsDisposed = true;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment