aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-04-30 23:46:17 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-04-30 23:46:17 -0400
commit2d49ec4a53708f103c72f4d968ac9759ce1cdf9c (patch)
treef7718e311011f876188f0caf1b878dcf1d7f266c /MediaBrowser.ServerApplication
parent780d5b914cc22bdf88477761e60c5de64b20504d (diff)
parent86919e84ee2bb89b8fc877c4e32379281c51877c (diff)
Merge branch 'beta'
Diffstat (limited to 'MediaBrowser.ServerApplication')
-rw-r--r--MediaBrowser.ServerApplication/MainStartup.cs19
-rw-r--r--MediaBrowser.ServerApplication/Native/Standby.cs40
-rw-r--r--MediaBrowser.ServerApplication/Native/WindowsApp.cs8
-rw-r--r--MediaBrowser.ServerApplication/ServerNotifyIcon.cs7
4 files changed, 56 insertions, 18 deletions
diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs
index cbf7d298c..dc61dcda8 100644
--- a/MediaBrowser.ServerApplication/MainStartup.cs
+++ b/MediaBrowser.ServerApplication/MainStartup.cs
@@ -273,11 +273,13 @@ namespace MediaBrowser.ServerApplication
}
private static ServerNotifyIcon _serverNotifyIcon;
+ private static TaskScheduler _mainTaskScheduler;
private static void ShowTrayIcon()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
_serverNotifyIcon = new ServerNotifyIcon(_appHost.LogManager, _appHost, _appHost.ServerConfigurationManager, _appHost.LocalizationManager);
+ _mainTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Application.Run();
}
@@ -321,6 +323,18 @@ namespace MediaBrowser.ServerApplication
}
}
+ public static void Invoke(Action action)
+ {
+ if (_isRunningAsService)
+ {
+ action();
+ }
+ else
+ {
+ Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, _mainTaskScheduler ?? TaskScheduler.Current);
+ }
+ }
+
/// <summary>
/// Starts the service.
/// </summary>
@@ -555,9 +569,10 @@ namespace MediaBrowser.ServerApplication
private static void ShutdownWindowsApplication()
{
- _logger.Info("Calling Application.Exit");
- Application.Exit();
+ //_logger.Info("Calling Application.Exit");
+ //Application.Exit();
+ _logger.Info("Calling Environment.Exit");
Environment.Exit(0);
_logger.Info("Calling ApplicationTaskCompletionSource.SetResult");
diff --git a/MediaBrowser.ServerApplication/Native/Standby.cs b/MediaBrowser.ServerApplication/Native/Standby.cs
index 274c72b25..919709538 100644
--- a/MediaBrowser.ServerApplication/Native/Standby.cs
+++ b/MediaBrowser.ServerApplication/Native/Standby.cs
@@ -1,4 +1,5 @@
-using System.Runtime.InteropServices;
+using System;
+using System.Runtime.InteropServices;
namespace MediaBrowser.ServerApplication.Native
{
@@ -7,11 +8,33 @@ namespace MediaBrowser.ServerApplication.Native
/// </summary>
public static class Standby
{
- public static void PreventSystemStandby()
+ public static void PreventSleepAndMonitorOff()
{
- SystemHelper.ResetStandbyTimer();
+ NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED | NativeMethods.ES_DISPLAY_REQUIRED);
}
+ public static void PreventSleep()
+ {
+ NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
+ }
+
+ // Clear EXECUTION_STATE flags to allow the system to sleep and turn off monitor normally
+ public static void AllowSleep()
+ {
+ NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS);
+ }
+
+ internal static class NativeMethods
+ {
+ // Import SetThreadExecutionState Win32 API and necessary flags
+ [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+ public static extern uint SetThreadExecutionState(uint esFlags);
+ public const uint ES_CONTINUOUS = 0x80000000;
+ public const uint ES_SYSTEM_REQUIRED = 0x00000001;
+ public const uint ES_DISPLAY_REQUIRED = 0x00000002;
+ }
+
+ [Flags]
internal enum EXECUTION_STATE : uint
{
ES_NONE = 0,
@@ -21,16 +44,5 @@ namespace MediaBrowser.ServerApplication.Native
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000
}
-
- public class SystemHelper
- {
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
-
- public static void ResetStandbyTimer()
- {
- EXECUTION_STATE es = SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED);
- }
- }
}
}
diff --git a/MediaBrowser.ServerApplication/Native/WindowsApp.cs b/MediaBrowser.ServerApplication/Native/WindowsApp.cs
index 34f4315de..10cd59436 100644
--- a/MediaBrowser.ServerApplication/Native/WindowsApp.cs
+++ b/MediaBrowser.ServerApplication/Native/WindowsApp.cs
@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
+using System.Windows.Forms;
using CommonIO;
using MediaBrowser.Controller.Power;
using MediaBrowser.Server.Startup.Common.FFMpeg;
@@ -137,7 +138,12 @@ namespace MediaBrowser.ServerApplication.Native
public void PreventSystemStandby()
{
- Standby.PreventSystemStandby();
+ MainStartup.Invoke(Standby.PreventSleep);
+ }
+
+ public void AllowSystemStandby()
+ {
+ MainStartup.Invoke(Standby.AllowSleep);
}
public IPowerManagement GetPowerManagement()
diff --git a/MediaBrowser.ServerApplication/ServerNotifyIcon.cs b/MediaBrowser.ServerApplication/ServerNotifyIcon.cs
index fbb93b660..27816db5a 100644
--- a/MediaBrowser.ServerApplication/ServerNotifyIcon.cs
+++ b/MediaBrowser.ServerApplication/ServerNotifyIcon.cs
@@ -36,10 +36,15 @@ namespace MediaBrowser.ServerApplication
set
{
Action act = () => notifyIcon1.Visible = false;
- contextMenuStrip1.Invoke(act);
+ Invoke(act);
}
}
+ public void Invoke(Action action)
+ {
+ contextMenuStrip1.Invoke(action);
+ }
+
public ServerNotifyIcon(ILogManager logManager,
IServerApplicationHost appHost,
IServerConfigurationManager configurationManager,