Link to home
Create AccountLog in
Avatar of sej69
sej69

asked on

C# app to run the original instance instead of opening another program instance

I'm writing a daemon app that will not be terminated normally by the end user and will run in the background unattended.  However, the user can click on the icon in the taskbar and run the process manually.

I do not want two instances of the application to run.  If the user tries to open the application a second time I want the original application to open instead.  How do I accomplish this?
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

foreach (Process process in Process.GetProcesses())
     {
          if (process.MainWindowTitle == this.Text)
               return false;
     }
     return true;
Avatar of sej69
sej69

ASKER

I'm not sure I follow what you're saying there... Is that a check in the initialize for the main form?
Before creating instance of your first form, put this code in main
Avatar of sej69

ASKER


That's ideal code for seeing if the app is already running; thanks.  Do you know how I can maximize the already running in the taskbar app?

Code so far:

using System.Diagnostics;

namespace ...
{
 public partial...
 {
        private bool CheckInstance()
        {
            foreach (Process process in Process.GetProcesses())
            {
                if (process.MainWindowTitle == this.Text)
                    return false;
            }
            return true;
        }

        public MyApp()
        {
            if (CheckInstance() == true)  // I want to run only one instance of app.
            {
                MessageBox.Show(" SyncProf is already running in the taskbar.");
                return;
            }
        ...

Open in new window

Avatar of sej69

ASKER

Still trying to figure this out; I'll update when I've got a resolution.
ASKER CERTIFIED SOLUTION
Avatar of systan
systan
Flag of Philippines image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Shortened;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

//just replace namespace <applicationinstance>
namespace applicationinstance
{
    
    static class Program
    {

        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern int SetForegroundWindow(IntPtr hWnd);

[STAThread]

private static IntPtr GetCurrentInstanceWindowHandle()
{
IntPtr hWnd = IntPtr.Zero;
Process process = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(process.ProcessName);
foreach(Process _process in processes) {
if  (
    (_process.Id != process.Id) &&
    (_process.MainModule.FileName == process.MainModule.FileName) &&
    (_process.MainWindowHandle != IntPtr.Zero)
    ) {
hWnd =_process.MainWindowHandle;
break;
}
}
return hWnd;
}

        static void Main()
        {

            IntPtr hWnd = GetCurrentInstanceWindowHandle();
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 1);
                SetForegroundWindow(hWnd);
                return;
            }

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

        }
    }
}

Open in new window

thank you,  i hope you like it.