0

i have a windows desktop application with c# and framework 4.6 the app run in system tray and there is a shortcut icon on desktop when user click the icon to run it if the app already run dont run new instance, only show (bring to front) existing app

public sealed class SingleInstance
{
    private const int SW_HIDE = 0;
    private const int SW_SHOW = 5;

    public static bool AlreadyRunning()
    {
        bool running = false;
        try
        {
            // Getting collection of process  
            Process currentProcess = Process.GetCurrentProcess();

            // Check with other process already running   
            foreach (var p in Process.GetProcesses())
            {
                if (p.Id != currentProcess.Id) // Check running process   
                {
                    if (p.ProcessName.Equals(currentProcess.ProcessName) == true)
                    {
                        running = true;
                        IntPtr hFound = p.MainWindowHandle;
                        if (User32API.IsIconic(hFound)) // If application is in ICONIC mode then  
                            User32API.ShowWindow(hFound, User32API.SW_RESTORE);
                        User32API.SetForegroundWindow(hFound); // Activate the window, if process is already running  
                        break;
                    }
                }
            }
        }
        catch { }

        return running;
    }
}

public class User32API
{
    [DllImport("User32.dll")]
    public static extern bool IsIconic(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    public const int SW_SHOW = 5;
    public const int SW_RESTORE = 9;
}

if app run back of the any windows it can show (bring to front) but it in system tray cant show

EDIT: minimize to tray

private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                Hide();
            }
        }

to open

 private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }
ismail uzunok
  • 163
  • 1
  • 1
  • 14
  • The VisualBasic dlls have a useful facility for this, see https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/development-with-my/overview-of-the-visual-basic-application-model but in essence I recall that you import the VB dll, use its application base rather than the default one, set the app to be single instance, add a handler to StartupNextInstance and any atttenpts to start another instance instead cause the event handler to fire. I think it even passes the command line arguments to the running instance – Caius Jard May 17 '21 at 08:17
  • When you minimize to tray, how do you do it? – Caius Jard May 17 '21 at 08:25
  • Does this answer your question? [Return to an already open application when a user tries to open a new instance](https://stackoverflow.com/questions/94274/return-to-an-already-open-application-when-a-user-tries-to-open-a-new-instance) –  May 17 '21 at 08:47
  • private void Form1_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { Hide(); } } private void notifyIcon1_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; this.Show(); this.WindowState = FormWindowState.Normal; } – ismail uzunok May 17 '21 at 10:44

1 Answers1

1

You can implement a Mutex to make sure there is only one instance of your application running. You can also use it to communicate between instances so the second instance can notify the first one that the user attempted to start your app a second time.

There is already a really good answer to this over here: https://stackoverflow.com/a/522874/14877741

It explains the Mutex concept a lot better than I could and it even has an example that brings the application window back to the foreground.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • thanks it works but i change Show method to bring the form front this.WindowState = FormWindowState.Minimized; this.Show(); this.WindowState = FormWindowState.Normal; – ismail uzunok May 18 '21 at 07:28
  • protected override void WndProc(ref Message m) works only in run as administrator mode – ismail uzunok May 21 '21 at 12:11
  • but i cant works the app in run as admin mode because it works in startup and on starup it ask to run program – ismail uzunok May 21 '21 at 12:12