Skip to content

Commit

Permalink
launcher single instance app
Browse files Browse the repository at this point in the history
  • Loading branch information
S74nk0 committed Jan 24, 2020
1 parent aaedb93 commit 4b6a781
Showing 1 changed file with 87 additions and 56 deletions.
143 changes: 87 additions & 56 deletions src/NiceHashMinerLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -79,6 +80,8 @@ private static void ClearAllTmpFiles()
ClearAllXXFiles("tmp.*");
}

private static Mutex _mutex = null;

private static string GetLatestApp()
{
var path = GetRootPath();
Expand Down Expand Up @@ -147,6 +150,16 @@ private static string GetLatestUpdater()
return null;
}

public static string GetHashString()
{
var appPathBytes = Encoding.UTF8.GetBytes(Assembly.GetExecutingAssembly().Location);
StringBuilder sb = new StringBuilder();
foreach (byte b in appPathBytes)
sb.Append(b.ToString("X2"));

return sb.ToString();
}

private async void App_OnStartup(object sender, StartupEventArgs e)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Expand Down Expand Up @@ -225,72 +238,90 @@ private async void App_OnStartup(object sender, StartupEventArgs e)
}
if (isUpdated) await Task.Delay(500); // so we release the temp files

ClearAllTmpFiles();

// TODO pass parent process PID
var latestAppDir = GetLatestApp();
var nhmApp = GetRootPath(latestAppDir, "NiceHashMiner.exe");
var args = "-lc";
if (isUpdated) args += " -updated";
var startInfo = new ProcessStartInfo
{
FileName = nhmApp,
Arguments = args,
WindowStyle = ProcessWindowStyle.Normal
};
var run = true;
while (run)
bool createdNew = false;
try
{
run = false;
try
string appPath = GetHashString();
_mutex = new Mutex(true, appPath, out createdNew);
if (!createdNew)
{
//MessageBox.Show("We have detected you are already running NiceHash Miner. Only a single instance should be running at a time.", "NiceHash Miner Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
// shutdown
Shutdown();
return;
}
base.OnStartup(e);

ClearAllTmpFiles();

// TODO pass parent process PID
var latestAppDir = GetLatestApp();
var nhmApp = GetRootPath(latestAppDir, "NiceHashMiner.exe");
var args = "-lc";
if (isUpdated) args += " -updated";
var startInfo = new ProcessStartInfo
{
FileName = nhmApp,
Arguments = args,
WindowStyle = ProcessWindowStyle.Normal
};
var run = true;
while (run)
{
using (var niceHashMiner = new Process { StartInfo = startInfo })
run = false;
try
{
var hasStarted = niceHashMiner?.Start();
niceHashMiner?.WaitForExit();
// TODO
Console.WriteLine(niceHashMiner.ExitCode);
// if exit code is 0 then check runasadmin or restart
if (IsRunAsAdmin())
{
RunAsAdmin.SelfElevate();
}
else if (IsRestart())
{
ClearAllDoFiles();
run = true;
}
else if (IsUpdate())
using (var niceHashMiner = new Process { StartInfo = startInfo })
{
run = true; // mark to false if updating doesn't fail
ClearAllDoFiles();
var exePath = Assembly.GetExecutingAssembly().Location;
var randomPart = DateTime.UtcNow.Millisecond;
var tmpLauncher = GetRootPath($"tmp.nhm_updater_{randomPart}.exe");
File.Copy(exePath, tmpLauncher, true);
var doUpdate = new Process
var hasStarted = niceHashMiner?.Start();
niceHashMiner?.WaitForExit();
// TODO
Console.WriteLine(niceHashMiner.ExitCode);
// if exit code is 0 then check runasadmin or restart
if (IsRunAsAdmin())
{
StartInfo = new ProcessStartInfo
RunAsAdmin.SelfElevate();
}
else if (IsRestart())
{
ClearAllDoFiles();
run = true;
}
else if (IsUpdate())
{
run = true; // mark to false if updating doesn't fail
ClearAllDoFiles();
var exePath = Assembly.GetExecutingAssembly().Location;
var randomPart = DateTime.UtcNow.Millisecond;
var tmpLauncher = GetRootPath($"tmp.nhm_updater_{randomPart}.exe");
File.Copy(exePath, tmpLauncher, true);
var doUpdate = new Process
{
FileName = tmpLauncher,
Arguments = "-update",
WindowStyle = ProcessWindowStyle.Normal
}
};
var updateStarted = doUpdate.Start();
run = !updateStarted; // set if we are good
StartInfo = new ProcessStartInfo
{
FileName = tmpLauncher,
Arguments = "-update",
WindowStyle = ProcessWindowStyle.Normal
}
};
var updateStarted = doUpdate.Start();
run = !updateStarted; // set if we are good
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// shutdown
Shutdown();
return;
}
finally
{
if (createdNew) _mutex?.ReleaseMutex();
}

// shutdown
Shutdown();
return;
}

public static async Task<bool> UnzipFileAsync(string zipLocation, string unzipLocation, IProgress<int> progress, CancellationToken stop)
Expand Down

0 comments on commit 4b6a781

Please sign in to comment.