Solved

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

Posted on 2014-01-23
4
257 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
[X]
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
  • 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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…

726 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