Solved

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

Posted on 2014-01-23
4
248 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now