Skip to content

Commit

Permalink
mutex bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Daudré-Vignier committed Dec 22, 2019
1 parent fbe25b4 commit d056786
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<local:ProxyValidationRule ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>
</DataGrid>
<tb:TaskbarIcon x:Name="taskBarIcon" Visibility="Visible" LeftClickCommand="{Binding TaskBarIcon_LeftClick}">
<tb:TaskbarIcon x:Name="taskBarIcon" Visibility="Hidden" LeftClickCommand="{Binding TaskBarIcon_LeftClick}">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu Name="MainMenu" Padding="-30, 0, 0, 0" Background="White">
<ContextMenu.Style>
Expand Down
24 changes: 14 additions & 10 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

namespace SystrayProxyManager
{
enum CloseFrom { Window, Menu, Other };
enum ExitOrigin { User, Mutex, None};
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ExitOrigin exitOrigin = ExitOrigin.None;
private Proxies proxies = new Proxies();
private bool UserExit = false;
private Proxy userDefinedProxyServer;
private bool userDefinedProxyState;
private DispatcherTimer timer;
Expand All @@ -26,11 +26,12 @@ public MainWindow()
if (!mutex.WaitOne(TimeSpan.Zero, true))
{
MessageBox.Show("only one instance at a time");
UserExit = true;
exitOrigin = ExitOrigin.Mutex;
this.Close();
return;
}
SetTimer();
taskBarIcon.Visibility = Visibility.Visible;
userDefinedProxyState = proxies.CurrentProxyState;
userDefinedProxyServer = proxies.CurrentProxyServer;
taskBarIcon.LeftClickCommand = new TaskBarIcon_LeftClick(proxies, taskBarIcon);
Expand Down Expand Up @@ -84,7 +85,7 @@ private void Timer_Elapsed(object sender, EventArgs e)
#region MainMenu click events
private void Exit_Click(Object sender, RoutedEventArgs e)
{
UserExit = true;
exitOrigin = ExitOrigin.User;
this.Close();
}

Expand Down Expand Up @@ -126,10 +127,10 @@ private void SetProxy_Click(Object sender, EventArgs e)
}
#endregion MainMenu click events

#region MainWindow close events
#region MainWindow events
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!UserExit)
if (exitOrigin == ExitOrigin.None)
{
UpdateSetProxyMenu();
this.Hide();
Expand All @@ -140,11 +141,14 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs

private void Window_Closed(object sender, EventArgs e)
{
timer.Stop();
proxies.SaveProxies();
mutex.ReleaseMutex();
if (exitOrigin == ExitOrigin.User)
{
timer.Stop();
proxies.SaveProxies();
mutex.ReleaseMutex();
}
}
#endregion MainWindow close event
#endregion MainWindow events

private void CenterWindowOnScreen()
{
Expand Down

0 comments on commit d056786

Please sign in to comment.