public void Start()
    {
      Log.Info("PS: Starting PowerScheduler client plugin...");

      try
      {
        // Register for MediaPortal actions to see if user is active
        GUIWindowManager.OnNewAction += new OnActionHandler(this.OnAction);

        // Create the timer that will wakeup the system after a specific amount of time after the
        // system has been put into standby
        _wakeupTimer = new WaitableTimer();

        // Load settings
        LoadSettings();

        // Unmute master volume
        if (_settings.GetSetting("UnmuteMasterVolume").Get<bool>())
        {
          try
          {
            using (MasterVolume _masterVolume = new MasterVolume())
            {
              if (_masterVolume.IsMuted)
              {
                Log.Debug("PS: Master volume is muted - unmute it");
                _masterVolume.IsMuted = false;
              }
            }
          }
          catch (Exception ex)
          {
            Log.Error("PS: Error in handling master volume - {0}", ex.Message);
          }
        }

        // Register as global service provider instance
        if (!GlobalServiceProvider.Instance.IsRegistered<IPowerScheduler>())
        {
          GlobalServiceProvider.Instance.Add<IPowerScheduler>(this);
        }
        Log.Debug("PS: Registered PowerScheduler as IPowerScheduler service to GlobalServiceProvider");

        // Register standby/wakeup handlers
        _wakeableStandbyHandler = new WakeableStandbyHandler();
        Register(_wakeableStandbyHandler);
        _wakeableWakeupHandler = new WakeableWakeupHandler();
        Register(_wakeableWakeupHandler);
        if (!_singleSeat)
        {
          // Register special standby/wakeup handlers (remote client only)
          _factory = new PowerSchedulerFactory();
          _factory.CreateDefaultSet();
        }
        Log.Debug("PS: Registered standby/wakeup handlers to PowerScheduler client plugin");

        // Register PSClientPlugin as standby / wakeup handler for PowerScheduler server (single-seat only)
        if (_singleSeat)
          RegisterToRemotePowerScheduler();

        // Create the EventWaitHandles for the StandbyWakeupThread and GetCurrentState
        _standbyWakeupTriggered = new EventWaitHandle(false, EventResetMode.AutoReset, "MediaPortal.PowerScheduler.StandbyWakeupTriggered");
        _standbyWakeupFinished = new AutoResetEvent(false);
        _standbyWakeupSuspend = new AutoResetEvent(false);  // initially do not take suspend branch
        _standbyWakeupResume = new ManualResetEvent(true);  // initially releases (no block)

        // Start the StandbyWakeupThread responsible for checking standby conditions and setting the wakeup timer
        _parentThread = Thread.CurrentThread;
        _standbyWakeupThread = new Thread(StandbyWakeupThread);
        _standbyWakeupThread.Name = "PS StandbyWakeup";
        _standbyWakeupThread.Start();
        Log.Debug("PS: StandbyWakeupThread started");

        SendPowerSchedulerEvent(PowerSchedulerEventType.Started);
        Log.Info("PS: PowerScheduler client plugin started");
      }
      catch (Exception ex)
      {
        Log.Error("PS: Exception in Start: {0}", ex);
        Stop();
      }
    }
    public void Stop()
    {
      Log.Info("PS: Stopping PowerScheduler client plugin...");

      try
      {
        // Stop and remove the StandbyWakeupThread
        if (_standbyWakeupThread != null)
        {
          _standbyWakeupThread.Abort();
          _standbyWakeupThread.Join(100);
          _standbyWakeupThread = null;
        }

        // Stop and remove the RestoreMuteThread
        if (_restoreMuteThread != null)
        {
          _restoreMuteThread.Abort();
          _restoreMuteThread.Join(100);
          _restoreMuteThread = null;
        }

        // Remove the EventWaitHandles
        if (_standbyWakeupTriggered != null)
        {
          _standbyWakeupTriggered.Close();
          _standbyWakeupTriggered = null;
        }
        if (_standbyWakeupSuspend != null)
        {
          _standbyWakeupSuspend.Close();
          _standbyWakeupSuspend = null;
        }
        if (_standbyWakeupResume != null)
        {
          _standbyWakeupResume.Close();
          _standbyWakeupResume = null;
        }
        if (_standbyWakeupFinished != null)
        {
          _standbyWakeupFinished.Close();
          _standbyWakeupFinished = null;
        }

        // Unregister PSClientPlugin as standby / wakeup handler (single-seat only)
        if (_singleSeat)
          UnregisterFromRemotePowerScheduler();

        // Remove the standby/resume handlers
        if (_factory != null)
        {
          _factory.RemoveDefaultSet();
          _factory = null;
        }
        Unregister(_wakeableStandbyHandler);
        Unregister(_wakeableWakeupHandler);
        Log.Debug("PS: Removed standby/wakeup handlers");

        // Unregister as global service provider instance
        if (GlobalServiceProvider.Instance.IsRegistered<IPowerScheduler>())
        {
          GlobalServiceProvider.Instance.Remove<IPowerScheduler>();
          Log.Debug("PS: Unregistered IPowerScheduler service from GlobalServiceProvider");
        }

        // Disable the wakeup timer
        if (_wakeupTimer != null)
        {
          _wakeupTimer.TimeToWakeup = DateTime.MaxValue;
          _wakeupTimer.Close();
          _wakeupTimer = null;
        }

        // Unregister MediaPortal actions
        GUIWindowManager.OnNewAction -= new OnActionHandler(this.OnAction);

        SendPowerSchedulerEvent(PowerSchedulerEventType.Stopped);
        Log.Info("PS: PowerScheduler client plugin stopped");
      }
      catch (Exception ex)
      {
        Log.Error("PS: Exception in Stop: {0}", ex);
      }
    }
    public void Start()
    {
      Log.Info("Starting PowerScheduler client plugin...");
      if (!LoadSettings())
      {
        return;
      }

      if (GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_PSCLIENTPLUGIN_UNATTENDED) == null)
      {
        GUIWindow win = new UnattendedWindow();
        try
        {
          win.Init();
        }
        catch (Exception ex)
        {
          Log.Error("Error initializing window:{0} {1} {2} {3}", win.ToString(), ex.Message, ex.Source, ex.StackTrace);
        }
        GUIWindowManager.Add(ref win);
      }

      GUIWindowManager.OnNewAction += new OnActionHandler(this.OnAction);
      // Create the timer that will wakeup the system after a specific amount of time after the
      // system has been put into standby
      _wakeupTimer = new WaitableTimer();
      _wakeupTimer.OnTimerExpired += new WaitableTimer.TimerExpiredHandler(OnWakeupTimerExpired);
      if (!GlobalServiceProvider.Instance.IsRegistered<IPowerScheduler>())
      {
        GlobalServiceProvider.Instance.Add<IPowerScheduler>(this);
      }
      SendPowerSchedulerEvent(PowerSchedulerEventType.Started);
      Log.Info("PowerScheduler client plugin started");

      _defaultExitWindows = WindowsController.HookExitWindows(SafeExitWindows);

      OnResume();
    }
    public void Stop()
    {
      Log.Info("PS: Stopping PowerScheduler server plugin...");

      try
      {
        // Stop and remove the StandbyWakeupThread
        if (_standbyWakeupThread != null)
        {
          _standbyWakeupThread.Abort();
          _standbyWakeupThread.Join(100);
          _standbyWakeupThread = null;
        }

        // Remove the EventWaitHandles
        if (_standbyWakeupTriggered != null)
        {
          _standbyWakeupTriggered.Close();
          _standbyWakeupTriggered = null;
        }
        if (_standbyWakeupSuspend != null)
        {
          _standbyWakeupSuspend.Close();
          _standbyWakeupSuspend = null;
        }
        if (_standbyWakeupResume != null)
        {
          _standbyWakeupResume.Close();
          _standbyWakeupResume = null;
        }
        if (_standbyWakeupFinished != null)
        {
          _standbyWakeupFinished.Close();
          _standbyWakeupFinished = null;
        }

        // Unregister power event handler
        UnRegisterPowerEventHandler();

        // Remove the standby/resume handlers
        if (_factory != null)
        {
          _factory.RemoveDefaultSet();
          _factory = null;
        }
        Unregister(_remoteClientStandbyHandler);
        Unregister(_powerControllerWakeupHandler);
        Unregister(_powerControllerStandbyHandler);
        Log.Debug("PS: Removed standby/wakeup handlers");

        // Disable the wakeup timer
        if (_wakeupTimer != null)
        {
          _wakeupTimer.TimeToWakeup = DateTime.MaxValue;
          _wakeupTimer.Close();
          _wakeupTimer = null;
        }

        SendPowerSchedulerEvent(PowerSchedulerEventType.Stopped);
        Log.Info("PS: PowerScheduler server plugin stopped");
      }
      catch (Exception ex)
      {
        Log.Error("PS: Exception in Stop: {0}", ex);
        Log.Info("PS: Exception in Stop: {0}", ex);
      }
    }
    public void Start(IController controller)
    {
      Log.Info("PS: Starting PowerScheduler server plugin...");

      try
      {
        // Save controller
        _controller = controller;

        // Create the timer that will wakeup the system after a specific amount of time after the
        // system has been put into standby
        _wakeupTimer = new WaitableTimer();

        // Load settings
        LoadSettings();

        // Register standby/wakeup handlers
        _powerControllerStandbyHandler = new PowerControllerStandbyHandler();
        Register(_powerControllerStandbyHandler);
        _powerControllerWakeupHandler = new PowerControllerWakeupHandler();
        Register(_powerControllerWakeupHandler);
        _remoteClientStandbyHandler = new RemoteClientStandbyHandler();
        Register(_remoteClientStandbyHandler);
        _factory = new PowerSchedulerFactory(controller);
        _factory.CreateDefaultSet();
        Log.Debug("PS: Registered standby/wakeup handlers to PowerScheduler server plugin");

        // Register power event handler to TVServer
        RegisterPowerEventHandler();

        // Register PowerScheduler as IPowerControl remoting service
        StartRemoting();

        // Create the EventWaitHandles for the StandbyWakeupThread and GetCurrentState
        _standbyWakeupTriggered = new EventWaitHandle(false, EventResetMode.AutoReset, "TvEngine.PowerScheduler.StandbyWakeupTriggered");
        _standbyWakeupFinished = new AutoResetEvent(false);
        _standbyWakeupSuspend = new AutoResetEvent(false);  // initially do not take suspend branch
        _standbyWakeupResume = new ManualResetEvent(true);  // initially releases (no block)

        // Start the StandbyWakeupThread responsible for checking standby conditions and setting the wakeup timer
        _parentThread = Thread.CurrentThread;
        _standbyWakeupThread = new Thread(StandbyWakeupThread);
        _standbyWakeupThread.Name = "PS StandbyWakeup";
        _standbyWakeupThread.Start();
        Log.Debug("PS: StandbyWakeupThread started");

        SendPowerSchedulerEvent(PowerSchedulerEventType.Started);
        Log.Info("PS: PowerScheduler server plugin started");
      }
      catch (Exception ex)
      {
        Log.Error("PS: Exception in Start: {0}", ex);
        Log.Info("PS: Exception in Start: {0}", ex);
        Stop();
      }
    }
 /// <summary>
 /// Called when the wakeup fails in any way
 /// </summary>
 private void OnWakeupTimerException(WaitableTimer sender, TimerException exception)
 {
   Log.Error("PowerScheduler: WaitableTimer had an exception:");
   Log.Write(exception);
 }
    public void Start(IController controller)
    {
      try
      {
        string threadname = Thread.CurrentThread.Name;
        if (string.IsNullOrEmpty(threadname))
          Thread.CurrentThread.Name = "Powerscheduler";
      }
      catch (Exception ex)
      {
        Log.Error("Powerscheduler: Error naming thread - {0}", ex.Message);
      }

      _controller = controller;

      Register(_clientStandbyHandler);
      Register(_clientWakeupHandler);
      RegisterPowerEventHandler();

      Log.Debug("PowerScheduler: Registered default set of standby/resume handlers to PowerScheduler");

      // Create the PowerManager that helps setting the correct thread executation state
      _powerManager = new PowerManager();

      // Create the timer that will wakeup the system after a specific amount of time after the
      // system has been put into standby
      if (_wakeupTimer == null)
      {
        _wakeupTimer = new WaitableTimer();
        _wakeupTimer.OnTimerExpired += new WaitableTimer.TimerExpiredHandler(OnWakeupTimerExpired);
        _wakeupTimer.OnTimerException += new WaitableTimer.TimerExceptionHandler(OnWakeupTimerException);
      }

      // Configure remoting if not already done
      StartRemoting();

      LoadSettings();

      // Create the default set of standby/resume handlers
      if (_factory == null)
        _factory = new PowerSchedulerFactory(controller);
      _factory.CreateDefaultSet();

      SendPowerSchedulerEvent(PowerSchedulerEventType.Started);

      Log.Debug("PowerScheduler: Starting poll thread");
      _stopThread = new ManualResetEvent(false);
      _pollThread = new Thread(new ThreadStart(PollThread));
      _pollThread.Name = "PowerScheduler poll thread";
      _pollThread.IsBackground = true;
      _pollThread.Priority = ThreadPriority.Normal;
      _pollThread.Start();

      Log.Info("Powerscheduler: started");
    }