Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

How to determine if an instance of another application is allready running.

Hi,

I have a button that opens the character map. But when you click on the same button again I get the Character Map twice. How can i prevent to start an application from running twice.

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("charmap.exe");
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }

Open in new window


Peter
0
peterkiers
Asked:
peterkiers
  • 2
3 Solutions
 
Naman GoelCommented:
Search for that process in running process list and check if it is there then don't run that process.
0
 
Matthew KellyCommented:
Using System.Diagnostics.Process class:

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (System.Diagnostics.Process.GetProcessesByName("charmap").Length == 0)
                {
                    System.Diagnostics.Process.Start("charmap.exe");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }

Open in new window

0
 
jmcmunnCommented:
This code will find the process, then make it focus if it is running.  Else it will launch it.

private void button1_Click(object sender, EventArgs e)
        {
            Process[] processlist = Process.GetProcesses();

            int processId = -1;
            foreach (Process theprocess in processlist)
            {
                if (theprocess.ProcessName.Equals("charmap"))
                {
                    processId = theprocess.Id;
                    break;
                }
            }

            if (processId != -1)
            {
                FocusProcessWindow(processId);
            }
            else
            {
                System.Diagnostics.Process.Start("charmap.exe");
            }
        }

        private static void FocusProcessWindow(int processID)
        {
            try
            {
                Process p = Process.GetProcessById(processID);

                ShowWindow(p.MainWindowHandle, 1);
                SetWindowPos(p.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3); //make top most
                SetWindowPos(p.MainWindowHandle, new IntPtr(-2), 0, 0, 0, 0, 3); //no longer top most
            }
            catch
            {
            }
        }

Open in new window

0
 
Naman GoelCommented:
both the solution's are write just adding some more checks:

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
               System.Diagnostics.Process[] runningProcesses=System.Diagnostics.Process.GetProcessesByName("charmap");
                if(runningProcesses!=null && runningProcesses.Length>0)
                {
                    System.Diagnostics.Process.Start("charmap.exe");
                }

               
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }
        }
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now