Fixed: Tray app restart

Co-Authored-By: ta264 <ta264@users.noreply.github.com>
pull/650/head
Qstick 3 years ago
parent b80bfaeff0
commit 9b9d2f2798

@ -4,7 +4,6 @@
<TargetFrameworks>net6.0</TargetFrameworks> <TargetFrameworks>net6.0</TargetFrameworks>
<ApplicationIcon>..\NzbDrone.Host\Prowlarr.ico</ApplicationIcon> <ApplicationIcon>..\NzbDrone.Host\Prowlarr.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="!$(RuntimeIdentifier.StartsWith('win'))"> <PropertyGroup Condition="!$(RuntimeIdentifier.StartsWith('win'))">
<AssemblyName>Prowlarr</AssemblyName> <AssemblyName>Prowlarr</AssemblyName>

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true" />
</runtime>
</configuration>

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
</assembly>

@ -19,7 +19,6 @@ namespace NzbDrone.Host
private readonly IBrowserService _browserService; private readonly IBrowserService _browserService;
private readonly IProcessProvider _processProvider; private readonly IProcessProvider _processProvider;
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly IUtilityModeRouter _utilityModeRouter;
private readonly Logger _logger; private readonly Logger _logger;
public AppLifetime(IHostApplicationLifetime appLifetime, public AppLifetime(IHostApplicationLifetime appLifetime,
@ -29,7 +28,6 @@ namespace NzbDrone.Host
IBrowserService browserService, IBrowserService browserService,
IProcessProvider processProvider, IProcessProvider processProvider,
IEventAggregator eventAggregator, IEventAggregator eventAggregator,
IUtilityModeRouter utilityModeRouter,
Logger logger) Logger logger)
{ {
_appLifetime = appLifetime; _appLifetime = appLifetime;
@ -39,7 +37,6 @@ namespace NzbDrone.Host
_browserService = browserService; _browserService = browserService;
_processProvider = processProvider; _processProvider = processProvider;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_utilityModeRouter = utilityModeRouter;
_logger = logger; _logger = logger;
appLifetime.ApplicationStarted.Register(OnAppStarted); appLifetime.ApplicationStarted.Register(OnAppStarted);

@ -171,7 +171,18 @@ namespace NzbDrone.Host
return ApplicationModes.UninstallService; return ApplicationModes.UninstallService;
} }
if (OsInfo.IsWindows && WindowsServiceHelpers.IsWindowsService()) // IsWindowsService can throw sometimes, so wrap it
bool isWindowsService = false;
try
{
isWindowsService = WindowsServiceHelpers.IsWindowsService();
}
catch
{
// don't care
}
if (OsInfo.IsWindows && isWindowsService)
{ {
return ApplicationModes.Service; return ApplicationModes.Service;
} }

@ -5,7 +5,6 @@
<RuntimeIdentifiers>win-x64;win-x86</RuntimeIdentifiers> <RuntimeIdentifiers>win-x64;win-x86</RuntimeIdentifiers>
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>..\NzbDrone.Host\Prowlarr.ico</ApplicationIcon> <ApplicationIcon>..\NzbDrone.Host\Prowlarr.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources> <GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

@ -4,9 +4,8 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NLog;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes; using NzbDrone.Core.Lifecycle;
using NzbDrone.Host; using NzbDrone.Host;
namespace NzbDrone.SysTray namespace NzbDrone.SysTray
@ -14,28 +13,19 @@ namespace NzbDrone.SysTray
public class SystemTrayApp : Form, IHostedService public class SystemTrayApp : Form, IHostedService
{ {
private readonly IBrowserService _browserService; private readonly IBrowserService _browserService;
private readonly IRuntimeInfo _runtimeInfo; private readonly ILifecycleService _lifecycle;
private readonly IProcessProvider _processProvider;
private readonly NotifyIcon _trayIcon = new NotifyIcon(); private readonly NotifyIcon _trayIcon = new NotifyIcon();
private readonly ContextMenuStrip _trayMenu = new ContextMenuStrip(); private readonly ContextMenuStrip _trayMenu = new ContextMenuStrip();
public SystemTrayApp(IBrowserService browserService, IRuntimeInfo runtimeInfo, IProcessProvider processProvider) public SystemTrayApp(IBrowserService browserService, ILifecycleService lifecycle)
{ {
_browserService = browserService; _browserService = browserService;
_runtimeInfo = runtimeInfo; _lifecycle = lifecycle;
_processProvider = processProvider;
} }
public void Start() public void Start()
{ {
Application.ThreadException += OnThreadException;
Application.ApplicationExit += OnApplicationExit;
Application.SetHighDpiMode(HighDpiMode.PerMonitor);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
_trayMenu.Items.Add(new ToolStripMenuItem("Launch Browser", null, LaunchBrowser)); _trayMenu.Items.Add(new ToolStripMenuItem("Launch Browser", null, LaunchBrowser));
_trayMenu.Items.Add(new ToolStripSeparator()); _trayMenu.Items.Add(new ToolStripSeparator());
_trayMenu.Items.Add(new ToolStripMenuItem("Exit", null, OnExit)); _trayMenu.Items.Add(new ToolStripMenuItem("Exit", null, OnExit));
@ -69,12 +59,6 @@ namespace NzbDrone.SysTray
DisposeTrayIcon(); DisposeTrayIcon();
} }
protected override void OnClosed(EventArgs e)
{
Console.WriteLine("Closing");
base.OnClosed(e);
}
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
Visible = false; Visible = false;
@ -102,8 +86,7 @@ namespace NzbDrone.SysTray
private void OnExit(object sender, EventArgs e) private void OnExit(object sender, EventArgs e)
{ {
LogManager.Configuration = null; _lifecycle.Shutdown();
Environment.Exit(0);
} }
private void LaunchBrowser(object sender, EventArgs e) private void LaunchBrowser(object sender, EventArgs e)
@ -117,33 +100,17 @@ namespace NzbDrone.SysTray
} }
} }
private void OnApplicationExit(object sender, EventArgs e)
{
if (_runtimeInfo.RestartPending)
{
_processProvider.SpawnNewProcess(_runtimeInfo.ExecutingApplication, "--restart --nobrowser");
}
DisposeTrayIcon();
}
private void OnThreadException(object sender, EventArgs e)
{
DisposeTrayIcon();
}
private void DisposeTrayIcon() private void DisposeTrayIcon()
{ {
try if (_trayIcon == null)
{
_trayIcon.Visible = false;
_trayIcon.Icon = null;
_trayIcon.Visible = false;
_trayIcon.Dispose();
}
catch (Exception)
{ {
return;
} }
_trayIcon.Visible = false;
_trayIcon.Icon = null;
_trayIcon.Visible = false;
_trayIcon.Dispose();
} }
} }
} }

@ -16,21 +16,22 @@ namespace NzbDrone
public static void Main(string[] args) public static void Main(string[] args)
{ {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
try try
{ {
var startupArgs = new StartupContext(args); var startupArgs = new StartupContext(args);
NzbDroneLogger.Register(startupArgs, false, true); NzbDroneLogger.Register(startupArgs, false, true);
Bootstrap.Start(args, e => Bootstrap.Start(args, e => { e.ConfigureServices((_, s) => s.AddSingleton<IHostedService, SystemTrayApp>()); });
{
e.ConfigureServices((_, s) => s.AddSingleton<IHostedService, SystemTrayApp>());
});
} }
catch (Exception e) catch (Exception e)
{ {
Logger.Fatal(e, "EPIC FAIL: " + e.Message); Logger.Fatal(e, "EPIC FAIL: " + e.Message);
var message = string.Format("{0}: {1}", e.GetType().Name, e.Message); var message = string.Format("{0}: {1}", e.GetType().Name, e.ToString());
MessageBox.Show(text: message, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error, caption: "Epic Fail!"); MessageBox.Show(text: message, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error, caption: "Epic Fail!");
} }
} }

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true" />
</runtime>
</configuration>

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
</assembly>
Loading…
Cancel
Save