Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-01-23
4
252 Views
Last Modified: 2014-01-24
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
Comment
Question by:peterkiers
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39805319
Search for that process in running process list and check if it is there then don't run that process.
0
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 200 total points
ID: 39805321
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
 
LVL 12

Assisted Solution

by:jmcmunn
jmcmunn earned 200 total points
ID: 39805322
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
 
LVL 13

Assisted Solution

by:Naman Goel
Naman Goel earned 100 total points
ID: 39805340
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with preventing downloading a zip file 10 45
Expression Evaluater 3 37
Get distinct values from excel or comm seperated file 4 30
Iteration Help (Asp.net VB) 5 24
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question