Re-fix for hidden console CPU + added tray icon

New: Tray Icon visible when running without console window
pull/3113/head
Mark McDowall 12 years ago
parent 506899730a
commit 08e7490ac5

@ -1,17 +1,22 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Threading; using System.Threading;
namespace NzbDrone.Common namespace NzbDrone.Common
{ {
public class ConsoleProvider public class ConsoleProvider
{ {
public static bool IsConsoleApplication
{
get { return Console.In != StreamReader.Null; }
}
public virtual void WaitForClose() public virtual void WaitForClose()
{ {
while (true) while (true)
{ {
Console.ReadLine(); Console.ReadLine();
Thread.Sleep(250);
} }
} }

@ -74,7 +74,9 @@
<Reference Include="System.configuration" /> <Reference Include="System.configuration" />
<Reference Include="System.Configuration.Install" /> <Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.ServiceProcess" /> <Reference Include="System.ServiceProcess" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
</ItemGroup> </ItemGroup>
@ -100,6 +102,10 @@
<Compile Include="RestProvider.cs" /> <Compile Include="RestProvider.cs" />
<Compile Include="SecurityProvider.cs" /> <Compile Include="SecurityProvider.cs" />
<Compile Include="ServiceProvider.cs" /> <Compile Include="ServiceProvider.cs" />
<Compile Include="SysTray\SysTrayApp.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="SysTray\SysTrayProvider.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

@ -0,0 +1,79 @@
using System;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace NzbDrone.Common.SysTray
{
public class SysTrayApp : Form
{
private readonly ConfigFileProvider _configFileProvider;
private readonly ProcessProvider _processProvider;
private readonly IISProvider _iisProvider;
private readonly EnvironmentProvider _environmentProvider;
private readonly NotifyIcon _trayIcon = new NotifyIcon();
private readonly ContextMenu _trayMenu = new ContextMenu();
public SysTrayApp(ConfigFileProvider configFileProvider, ProcessProvider processProvider,
IISProvider iisProvider, EnvironmentProvider environmentProvider)
{
_configFileProvider = configFileProvider;
_processProvider = processProvider;
_iisProvider = iisProvider;
_environmentProvider = environmentProvider;
}
public SysTrayApp()
{
}
public void Create()
{
_trayMenu.MenuItems.Add("Launch Browser", LaunchBrowser);
_trayMenu.MenuItems.Add("-");
_trayMenu.MenuItems.Add("Exit", OnExit);
_trayIcon.Text = String.Format("NzbDrone - {0}", _environmentProvider.Version);
_trayIcon.Icon = new Icon(Assembly.GetEntryAssembly().GetManifestResourceStream("NzbDrone.NzbDrone.ico"));
_trayIcon.ContextMenu = _trayMenu;
_trayIcon.Visible = true;
}
protected override void OnClosed(EventArgs e)
{
Console.WriteLine("Closing");
base.OnClosed(e);
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
_trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void LaunchBrowser(object sender, EventArgs e)
{
_processProvider.Start(_iisProvider.AppUrl);
}
}
}

@ -0,0 +1,26 @@
using System.Linq;
using System.Windows.Forms;
namespace NzbDrone.Common.SysTray
{
public class SysTrayProvider
{
private readonly SysTrayApp _sysTrayApp;
public SysTrayProvider(SysTrayApp sysTrayApp)
{
_sysTrayApp = sysTrayApp;
}
public SysTrayProvider()
{
}
public virtual void Start()
{
_sysTrayApp.Create();
Application.Run(_sysTrayApp);
}
}
}

@ -110,7 +110,7 @@
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="NzbDrone.ico" /> <EmbeddedResource Include="NzbDrone.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0"> <BootstrapperPackage Include=".NETFramework,Version=v4.0">

@ -106,7 +106,7 @@
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="NzbDrone.ico" /> <EmbeddedResource Include="NzbDrone.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0"> <BootstrapperPackage Include=".NETFramework,Version=v4.0">

@ -2,9 +2,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net.Mime;
using NLog; using NLog;
using NzbDrone.Common; using NzbDrone.Common;
using NzbDrone.Common.SysTray;
using NzbDrone.Model; using NzbDrone.Model;
using NzbDrone.Providers;
namespace NzbDrone namespace NzbDrone
{ {
@ -17,16 +20,18 @@ namespace NzbDrone
private readonly ConsoleProvider _consoleProvider; private readonly ConsoleProvider _consoleProvider;
private readonly EnvironmentProvider _environmentProvider; private readonly EnvironmentProvider _environmentProvider;
private readonly ProcessProvider _processProvider; private readonly ProcessProvider _processProvider;
private readonly SysTrayProvider _sysTrayProvider;
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider,
ConsoleProvider consoleProvider, EnvironmentProvider environmentProvider, ConsoleProvider consoleProvider, EnvironmentProvider environmentProvider,
ProcessProvider processProvider) ProcessProvider processProvider, SysTrayProvider sysTrayProvider)
{ {
_applicationServer = applicationServer; _applicationServer = applicationServer;
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_consoleProvider = consoleProvider; _consoleProvider = consoleProvider;
_environmentProvider = environmentProvider; _environmentProvider = environmentProvider;
_processProvider = processProvider; _processProvider = processProvider;
_sysTrayProvider = sysTrayProvider;
} }
public void Route(IEnumerable<string> args) public void Route(IEnumerable<string> args)
@ -56,7 +61,14 @@ namespace NzbDrone
{ {
logger.Trace("Console selected"); logger.Trace("Console selected");
_applicationServer.Start(); _applicationServer.Start();
_consoleProvider.WaitForClose(); if(ConsoleProvider.IsConsoleApplication)
_consoleProvider.WaitForClose();
else
{
_sysTrayProvider.Start();
}
break; break;
} }
case ApplicationMode.InstallService: case ApplicationMode.InstallService:

Loading…
Cancel
Save