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
-1
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>
+12 -45
View File
@@ -4,9 +4,8 @@ using System.Threading;
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 @@ namespace NzbDrone.SysTray
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 @@ namespace NzbDrone.SysTray
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 @@ 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()
{
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();
}
}
}
+6 -5
View File
@@ -16,21 +16,22 @@ namespace NzbDrone
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!");
}
}
}
-52
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>