Skip to content

Commit

Permalink
Uninstaller: Try to shut down a running SyncTrayzor instances
Browse files Browse the repository at this point in the history
Fixes #516
  • Loading branch information
canton7 committed Apr 2, 2021
1 parent c5732c2 commit 23dfac1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions installer/common.iss
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,14 @@ begin
end;
end;
[UninstallRun]
Filename: "{app}\SyncTrayzor.exe"; Parameters: "--shutdown"; RunOnceId: "ShutdownSyncTrayzor"; Flags: skipifdoesntexist

[UninstallDelete]
Type: files; Name: "{app}\ProcessRunner.exe.old"
Type: files; Name: "{app}\InstallCount.txt"
; Not sure why this gets left behind, but it does. Clean it up...
Type: filesandordirs; Name: "{app}\locales"
Type: filesandordirs; Name: "{userappdata}\{#AppDataFolder}"
Type: filesandordirs; Name: "{localappdata}\{#AppDataFolder}"

21 changes: 21 additions & 0 deletions src/SyncTrayzor/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ protected override void Configure()
{
try
{
if (this.options.Shutdown)
{
client.Shutdown();
// Give it some time to shut down
var elapsed = Stopwatch.StartNew();
while (elapsed.Elapsed < TimeSpan.FromSeconds(10) &&
this.Container.Get<IIpcCommsClientFactory>().TryCreateClient() != null)
{
Thread.Sleep(100);
}
// Wait another half-second -- it seems it can take the browser process a little longer to exit
Thread.Sleep(500);
Environment.Exit(0);
}

if (this.options.StartSyncthing || this.options.StopSyncthing)
{
if (this.options.StartSyncthing)
Expand All @@ -142,6 +157,12 @@ protected override void Configure()
logger.Error(e, $"Failed to talk to {client}: {e.Message}. Pretending that it doesn't exist...");
}
}

// If we got this far, there probably isn't another instance running, and we should just shut down
if (this.options.Shutdown)
{
Environment.Exit(0);
}

var configurationProvider = this.Container.Get<IConfigurationProvider>();
configurationProvider.Initialize(AppSettings.Instance.DefaultUserConfiguration);
Expand Down
9 changes: 9 additions & 0 deletions src/SyncTrayzor/Services/CommandLineOptionsParser.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using Mono.Options;
using Stylet;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Windows;
using System.Windows.Forms;

namespace SyncTrayzor.Services
{
public class CommandLineOptionsParser
{
private readonly IWindowManager windowManager;

public bool Shutdown { get; private set; }
public bool StartMinimized { get; private set; }
public bool StartSyncthing { get; private set; }
public bool StopSyncthing { get; private set; }
Expand All @@ -30,6 +33,7 @@ public bool Parse(string[] args)
bool show = false;

var options = new OptionSet()
.Add("shutdown", "\nIf another SyncTrayzor process is running, tell it to shutdown.", v => this.Shutdown = true)
.Add("start-syncthing", "\nIf another SyncTrayzor process is running, tell it to start Syncthing. Otherwise, launch with Syncthing started regardless of configuration.", v => this.StartSyncthing = true)
.Add("stop-syncthing", "\nIf another SyncTrayzor process is running, tell it to stop Syncthing. Otherwise, launch with Syncthing stopped regardless of configuration.", v => this.StopSyncthing = true)
.Add("noautostart", null, v => this.StopSyncthing = true, hidden: true)
Expand All @@ -47,6 +51,11 @@ public bool Parse(string[] args)
return false;
}

if (this.Shutdown && (this.StartSyncthing || this.StopSyncthing || minimized || show))
{
this.windowManager.ShowMessageBox("--shutdown may not be used with any other options", "Error", icon: MessageBoxImage.Error);
return false;
}
if (this.StartSyncthing && this.StopSyncthing)
{
this.windowManager.ShowMessageBox("--start-syncthing and --stop-syncthing may not be used together", "Error", icon: MessageBoxImage.Error);
Expand Down
6 changes: 6 additions & 0 deletions src/SyncTrayzor/Services/Ipc/IpcCommsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public UnknownIpcCommandException(string message)

public interface IIpcCommsClient
{
void Shutdown();
void ShowMainWindow();
void StartSyncthing();
void StopSyncthing();
Expand All @@ -30,6 +31,11 @@ public IpcCommsClient(int pid)
this.pid = pid;
}

public void Shutdown()
{
this.SendCommand("Shutdown");
}

public void ShowMainWindow()
{
this.SendCommand("ShowMainWindow");
Expand Down
13 changes: 12 additions & 1 deletion src/SyncTrayzor/Services/Ipc/IpcCommsServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public class IpcCommsServer : IIpcCommsServer
private static readonly ILogger logger = LogManager.GetCurrentClassLogger();

private readonly ISyncthingManager syncthingManager;
private readonly IApplicationState applicationState;
private readonly IApplicationWindowState windowState;

private CancellationTokenSource cts;

public string PipeName => $"SyncTrayzor-{Process.GetCurrentProcess().Id}";

public IpcCommsServer(ISyncthingManager syncthingManager, IApplicationWindowState windowState)
public IpcCommsServer(ISyncthingManager syncthingManager, IApplicationState applicationState, IApplicationWindowState windowState)
{
this.syncthingManager = syncthingManager;
this.applicationState = applicationState;
this.windowState = windowState;
}

Expand Down Expand Up @@ -101,6 +103,10 @@ private string HandleReceivedCommand(string command)
{
switch (command)
{
case "Shutdown":
this.Shutdown();
return "OK";

case "ShowMainWindow":
this.ShowMainWindow();
return "OK";
Expand All @@ -118,6 +124,11 @@ private string HandleReceivedCommand(string command)
}
}

private void Shutdown()
{
this.applicationState.Shutdown();
}

private void ShowMainWindow()
{
this.windowState.EnsureInForeground();
Expand Down

0 comments on commit 23dfac1

Please sign in to comment.