Skip to content

Commit

Permalink
feat: Bring window to front on existing instance app launch (#136)
Browse files Browse the repository at this point in the history
This sends a socket message to the first instance that it should Initialize the frontend and bring it to front and then destroy the second instance (itself).
  • Loading branch information
Segergren authored Jul 25, 2023
1 parent 062fc5c commit 9aa458f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Classes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
#if !WINDOWS
using PhotinoNET;
#else
Expand Down Expand Up @@ -58,7 +60,17 @@ static void Main(string[] args) {
// prevent multiple instances
var mutex = new Mutex(true, @"Global\RePlays", out var createdNew);
if (!createdNew) {
Logger.WriteLine("RePlays is already running! Exiting the application.");
Logger.WriteLine("RePlays is already running! Exiting the application and bringing the other instance to foreground.");
try {
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
sender.Connect(new IPEndPoint(IPAddress.Loopback, 3333));
sender.Send(Encoding.UTF8.GetBytes("BringToForeground"));
Logger.WriteLine($"Sent BringToForeground to the other instance");
}
}
catch (Exception ex) {
Logger.WriteLine($"Socket client exception: {ex.Message}");
}
return;
}

Expand Down
46 changes: 46 additions & 0 deletions Classes/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static RePlays.Utils.Functions;
using System.Threading;

namespace RePlays {
public partial class frmMain : Form {
Expand Down Expand Up @@ -46,6 +50,9 @@ public frmMain() {
RecordingService.Start(typeof(LibObsRecorder));
}
GetAudioDevices();

Thread socketThread = new(StartSocketServer);
socketThread.Start();
}

private void frmMain_Load(object sender, System.EventArgs e) {
Expand Down Expand Up @@ -178,7 +185,46 @@ private void frmMain_Resize(object sender, System.EventArgs e) {
}
}

private static void StartSocketServer() {
int port = 3333;
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new(ipAddress, port);
Socket listener = new(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

try {
listener.Bind(localEndPoint);
listener.Listen(10);

while (true) {
Socket handler = listener.Accept();
string message = ReceiveMessage(handler);

if (message == "BringToForeground") {
Logger.WriteLine($"Got socket message: {message}");
frmMain.Instance.Invoke((MethodInvoker)(() => frmMain.Instance.InitializeWebView2()));
frmMain.Instance.Invoke((MethodInvoker)(() => frmMain.Instance.ActivateFormAndRestoreWindowState()));
}

handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
catch (Exception ex) {
Logger.WriteLine($"Socket server exception: {ex.Message}");
}
}

static string ReceiveMessage(Socket socket) {
byte[] buffer = new byte[1024];
int bytesRead = socket.Receive(buffer);
return Encoding.UTF8.GetString(buffer, 0, bytesRead);
}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e) {
ActivateFormAndRestoreWindowState();
}

private void ActivateFormAndRestoreWindowState() {
this.Activate();
if (this.WindowState == FormWindowState.Minimized) {
this.Opacity = 100;
Expand Down

0 comments on commit 9aa458f

Please sign in to comment.