Skip to content

Commit

Permalink
Prevent tray Exit while Closing event is running
Browse files Browse the repository at this point in the history
Also, upgrade NuGet packages
  • Loading branch information
menees committed Jun 1, 2024
1 parent 1ed0de8 commit d6eadf6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<_SkipUpgradeNetAnalyzersNuGetWarning >true</_SkipUpgradeNetAnalyzersNuGetWarning>

<!-- Make the assembly, file, and NuGet package versions the same. -->
<Version>1.1.4</Version>
<Version>1.1.5</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down Expand Up @@ -53,15 +54,15 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Menees.Analyzers" Version="3.0.5">
<PackageReference Include="Menees.Analyzers" Version="3.2.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions src/WirePeep.Common/WirePeep.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Menees.Common" Version="5.0.3" />
<PackageReference Include="Nullable" Version="1.3.0">
<PackageReference Include="Menees.Common" Version="5.1.2" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
51 changes: 39 additions & 12 deletions src/WirePeep/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed partial class MainWindow : ExtendedWindow, IDisposable
private StateManager? stateManager;
private Timer? backgroundTimer;
private int updatingLock;
private bool closing;
private ClosingState closingState;
private ConnectionState? simulateConnection;
private DataGrid? selectedGrid;
private Logger? logger;
Expand All @@ -61,7 +61,7 @@ public MainWindow()
this.InitializeComponent();

this.statusRows = (StatusRowCollection)this.Resources["StatusRows"];
this.statusRowMap = new Dictionary<Guid, StatusRow>();
this.statusRowMap = [];

this.logRows = (LogRowCollection)this.Resources["LogRows"];
this.failedPeerGroupToLogRowMap = new Dictionary<Guid, LogRow>(this.statusRowMap.Comparer);
Expand All @@ -75,6 +75,17 @@ public MainWindow()

#endregion

#region Private Enums

private enum ClosingState
{
None,
Requested,
Confirmed,
}

#endregion

#region Internal Properties

internal bool StartMinimized { get; set; }
Expand Down Expand Up @@ -143,7 +154,7 @@ internal void SaveNonWindowSettings()

private static void CopyToClipboard(DataGrid source, bool copyRow)
{
IList<DataGridCellInfo> copyCells = copyRow ? source.SelectedCells : new[] { source.CurrentCell };
IList<DataGridCellInfo> copyCells = copyRow ? source.SelectedCells : [source.CurrentCell];
StringBuilder sb = new();
foreach (DataGridCellInfo cell in copyCells)
{
Expand Down Expand Up @@ -212,7 +223,7 @@ private void UpdateStatusRows(StateSnapshot states)

private void UpdateLogRows(IEnumerable<PeerGroupState> peerGroupStates)
{
List<PeerGroupState> failedChanged = new(0);
List<PeerGroupState> failedChanged = [];

HashSet<Guid> currentPeerGroups = new(this.failedPeerGroupToLogRowMap.Comparer);
foreach (PeerGroupState peerGroupState in peerGroupStates)
Expand Down Expand Up @@ -397,12 +408,12 @@ private W.NotifyIcon CreateNotifyIcon()
notifyIcon.Visible = true;
notifyIcon.MouseDoubleClick += this.NotifyIconMouseDoubleClick;

notifyIconMenu.Items.AddRange(new W.ToolStripItem[]
{
notifyIconMenu.Items.AddRange(
[
notifyIconViewMenu,
notifyIconSeparator,
notifyIconExitMenu,
});
]);

// notifyIconMenu.ShowImageMargin = false;
notifyIconViewMenu.Font = new Font(notifyIconMenu.Font, System.Drawing.FontStyle.Bold);
Expand Down Expand Up @@ -435,22 +446,36 @@ private void WindowSaverSaveSettings(object? sender, SettingsEventArgs e)

private void WindowClosing(object? sender, CancelEventArgs e)
{
if (!this.IsSessionEnding && (this.appOptions?.ConfirmClose ?? false) && !e.Cancel)
if (!this.IsSessionEnding
&& (this.appOptions?.ConfirmClose ?? false)
&& !e.Cancel
&& this.closingState == ClosingState.None)
{
e.Cancel = !WindowsUtility.ShowQuestion(this, "Are you sure you want to exit?");
this.closingState = ClosingState.Requested;
try
{
e.Cancel = !WindowsUtility.ShowQuestion(this, "Are you sure you want to exit?");
}
finally
{
this.closingState = ClosingState.None;
}
}

if (!e.Cancel)
{
this.closing = true;
this.closingState = ClosingState.Confirmed;
this.backgroundTimer?.Dispose();
this.CloseLogger();
}
}

private void ExitExecuted(object? sender, ExecutedRoutedEventArgs e)
{
this.Close();
if (this.closingState == ClosingState.None)
{
this.Close();
}
}

private void ViewOptionsExecuted(object? sender, ExecutedRoutedEventArgs e)
Expand Down Expand Up @@ -523,7 +548,9 @@ private void SimulateConnectionExecuted(object? sender, ExecutedRoutedEventArgs
private void BackgroundTimerCallback(object? state)
{
// Only let one Update run at a time. If the callback takes longer than 1 second, it will be invoked again from another thread.
if (!this.closing && this.stateManager != null && Interlocked.CompareExchange(ref this.updatingLock, 1, 0) == 0)
if (this.closingState != ClosingState.Confirmed
&& this.stateManager != null
&& Interlocked.CompareExchange(ref this.updatingLock, 1, 0) == 0)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/WirePeep/WirePeep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Menees.Windows.Presentation" Version="5.1.1" />
<PackageReference Include="Menees.Windows.Presentation" Version="5.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d6eadf6

Please sign in to comment.