Skip to content

Commit

Permalink
Fix exiting wrapper, due to inaccessible process. (#22)
Browse files Browse the repository at this point in the history
* Update editor config to reflect current code styles

* Fix process permissions for some cases

* Properly handle any race conditions

* Resolve merge conflict

* Remove no longer needed method arguments check

* Simplify logging statement
  • Loading branch information
webbertakken authored Sep 10, 2020
1 parent 6b56768 commit 47233a8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
29 changes: 18 additions & 11 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
[*]
indent_style = tab
indent_style=tab

dotnet_diagnostic.CA1031.severity = none # CA1031: Do not catch general exception types
dotnet_diagnostic.CA1034.severity = none # CA1034: Nested types should not be visible
dotnet_diagnostic.CA1054.severity = none # CA1054: Uri parameters should not be strings
dotnet_diagnostic.CA1303.severity = none # CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1305.severity = none # CA1305: Specify IFormatProvider
dotnet_diagnostic.CA1308.severity = none # CA1308: Normalize strings to uppercase
dotnet_diagnostic.CA1819.severity = none # CA1819: Properties should not return arrays
dotnet_diagnostic.CA2007.severity = none # CA2007: Consider calling ConfigureAwait on the awaited task
dotnet_diagnostic.CA2229.severity = none # CA2229: Implement serialization constructors
dotnet_diagnostic.CS1591.severity = none # CS1591: Missing XML comment for publicly visible type or member
dotnet_diagnostic.ca1031.severity=none # CA1031: Do not catch general exception types
dotnet_diagnostic.ca1034.severity=none # CA1034: Nested types should not be visible
dotnet_diagnostic.ca1054.severity=none # CA1054: Uri parameters should not be strings
dotnet_diagnostic.ca1303.severity=none # CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.ca1305.severity=none # CA1305: Specify IFormatProvider
dotnet_diagnostic.ca1308.severity=none # CA1308: Normalize strings to uppercase
dotnet_diagnostic.ca1819.severity=none # CA1819: Properties should not return arrays
dotnet_diagnostic.ca2007.severity=none # CA2007: Consider calling ConfigureAwait on the awaited task
dotnet_diagnostic.ca2229.severity=none # CA2229: Implement serialization constructors
dotnet_diagnostic.cs1591.severity=none # CS1591: Missing XML comment for publicly visible type or member

# Microsoft .NET properties
csharp_indent_braces=false
csharp_new_line_before_open_brace=all

# ReSharper properties
resharper_csharp_case_block_braces=next_line
58 changes: 41 additions & 17 deletions windows-terminal-quake/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Forms;
using WindowsTerminalQuake.Native;
using WindowsTerminalQuake.UI;
using Serilog;

namespace WindowsTerminalQuake
{
Expand All @@ -20,22 +21,7 @@ public static void Main(string[] args)

try
{
Process process = Process.GetProcessesByName("WindowsTerminal").FirstOrDefault();
if (process == null)
{
process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "wt",
UseShellExecute = false,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Maximized
}
};
process.Start();
}

var process = GetOrCreateWindowsTerminalProcess();
process.EnableRaisingEvents = true;
process.Exited += (sender, e) =>
{
Expand All @@ -55,12 +41,50 @@ public static void Main(string[] args)
}
catch (Exception ex)
{
Log.Logger.Warning(ex, $"Error: {ex.Message}");
_trayIcon.Notify(ToolTipIcon.Error, $"Cannot start: '{ex.Message}'.");

Close();
}
}

private static Process GetOrCreateWindowsTerminalProcess()
{
var process = Process.GetProcessesByName("WindowsTerminal").FirstOrDefault();

if (process == null)
{
process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "wt",
UseShellExecute = false,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Maximized
}
};
process.Start();

try
{
// Note: Accessing mainWindowHandle already throws "Process has exited, so the requested information is not available."
if (process.MainWindowHandle == IntPtr.Zero)
{
throw new Exception("Can not access newly started process.");
}
}
catch (Exception)
{
process = Process.GetProcessesByName("WindowsTerminal").First();
// _process.WaitForInputIdle();
process.Refresh();
}
}

return process;
}

private static void Close()
{
_toggler?.Dispose();
Expand All @@ -70,4 +94,4 @@ private static void Close()
_trayIcon = null;
}
}
}
}

0 comments on commit 47233a8

Please sign in to comment.