Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If another instance of the app is allready running, it just opens it #389

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions Steam Desktop Authenticator/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public partial class MainForm : Form
// Forms
private TradePopupForm popupFrm = new TradePopupForm();

protected override void WndProc(ref System.Windows.Forms.Message message)
{
if (message.Msg == SingleInstance.WM_SHOWFIRSTINSTANCE)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
base.WndProc(ref message);
}

public MainForm()
{
InitializeComponent();
Expand Down Expand Up @@ -724,5 +734,10 @@ private void MainForm_KeyDown(object sender, KeyEventArgs e)
CopyLoginToken();
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not sure adding this

private void listAccounts_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}
98 changes: 71 additions & 27 deletions Steam Desktop Authenticator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using CommandLine;
using CommandLine.Text;


namespace Steam_Desktop_Authenticator
{

Expand All @@ -32,47 +31,92 @@ public string GetUsage()
}
}

static class Program
static public class WinApi
{
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);

public static int RegisterWindowMessage(string format, params object[] args)
{
string message = String.Format(format, args);
return RegisterWindowMessage(message);
}

public const int HWND_BROADCAST = 0xffff;
public const int SW_SHOWNORMAL = 1;

[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
}

static public class SingleInstance
{
public static Process PriorProcess()
// Returns a System.Diagnostics.Process pointing to
// a pre-existing process with the same name as the
// current one, if any; or null if the current process
// is unique.
public static readonly int WM_SHOWFIRSTINSTANCE = WinApi.RegisterWindowMessage("WM_SHOWFIRSTINSTANCE|{0}", ProgramInfo.AssemblyGuid);
static Mutex mutex;
static public bool Start()
{
bool onlyInstance = false;
string mutexName = String.Format("Local\\{0}", ProgramInfo.AssemblyGuid);

// if you want your app to be limited to a single instance
// across ALL SESSIONS (multiple users & terminal services), then use the following line instead:
// string mutexName = String.Format("Global\\{0}", ProgramInfo.AssemblyGuid);

mutex = new Mutex(true, mutexName, out onlyInstance);
return onlyInstance;
}
static public void ShowFirstInstance()
{
WinApi.PostMessage(
(IntPtr)WinApi.HWND_BROADCAST,
WM_SHOWFIRSTINSTANCE,
IntPtr.Zero,
IntPtr.Zero);
}
static public void Stop()
{
try
mutex.ReleaseMutex();
}
}

static public class ProgramInfo
{
static public string AssemblyGuid
{
get
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
if (attributes.Length == 0)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return p;
return String.Empty;
}
return null;
}
catch (Exception)
{
return null;
return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value;
}
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// run the program only once
if (PriorProcess() != null)
if (!SingleInstance.Start())
{
MessageBox.Show("Another instance of the app is already running.");
SingleInstance.ShowFirstInstance();
return;
}

// Parse command line arguments
var options = new Options();
Application.EnableVisualStyles();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this

Application.SetCompatibleTextRenderingDefault(false);



// Parse command line arguments
var options = new Options();
Parser.Default.ParseArguments(args, options);

Application.EnableVisualStyles();
Expand Down