Fixed: Tray app restart

(cherry picked from commit 5fce3bbedb48edc547d1e6a1db3af06b5542886d)
This commit is contained in:
ta264 2021-11-18 21:19:49 +00:00
parent dfb9558868
commit 04e575903f
8 changed files with 30 additions and 160 deletions

View file

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

View file

@ -1,52 +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">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on and is
is designed to work with. Uncomment the appropriate elements and Windows will
automatically selected the most compatible environment. -->
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
<!-- 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>

View file

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

View file

@ -180,7 +180,18 @@ public static ApplicationModes GetApplicationMode(IStartupContext startupContext
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;
}

View file

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

View file

@ -4,9 +4,8 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Extensions.Hosting;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Host;
namespace NzbDrone.SysTray
@ -14,28 +13,19 @@ namespace NzbDrone.SysTray
public class SystemTrayApp : Form, IHostedService
{
private readonly IBrowserService _browserService;
private readonly IRuntimeInfo _runtimeInfo;
private readonly IProcessProvider _processProvider;
private readonly ILifecycleService _lifecycle;
private readonly NotifyIcon _trayIcon = new NotifyIcon();
private readonly ContextMenuStrip _trayMenu = new ContextMenuStrip();
public SystemTrayApp(IBrowserService browserService, IRuntimeInfo runtimeInfo, IProcessProvider processProvider)
public SystemTrayApp(IBrowserService browserService, ILifecycleService lifecycle)
{
_browserService = browserService;
_runtimeInfo = runtimeInfo;
_processProvider = processProvider;
_lifecycle = lifecycle;
}
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 ToolStripSeparator());
_trayMenu.Items.Add(new ToolStripMenuItem("Exit", null, OnExit));
@ -69,12 +59,6 @@ protected override void OnClosing(CancelEventArgs e)
DisposeTrayIcon();
}
protected override void OnClosed(EventArgs e)
{
Console.WriteLine("Closing");
base.OnClosed(e);
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
@ -102,8 +86,7 @@ protected override void Dispose(bool isDisposing)
private void OnExit(object sender, EventArgs e)
{
LogManager.Configuration = null;
Environment.Exit(0);
_lifecycle.Shutdown();
}
private void LaunchBrowser(object sender, EventArgs e)
@ -117,33 +100,17 @@ private void LaunchBrowser(object sender, EventArgs e)
}
}
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()
{
try
{
_trayIcon.Visible = false;
_trayIcon.Icon = null;
_trayIcon.Visible = false;
_trayIcon.Dispose();
}
catch (Exception)
if (_trayIcon == null)
{
return;
}
_trayIcon.Visible = false;
_trayIcon.Icon = null;
_trayIcon.Visible = false;
_trayIcon.Dispose();
}
}
}

View file

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

View file

@ -1,52 +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">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on and is
is designed to work with. Uncomment the appropriate elements and Windows will
automatically selected the most compatible environment. -->
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
<!-- 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>