Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 44
How do I get the id from URL? 19 48
Hovering effect 9 29
Change data in datatable 8 18
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

20 Experts available now in Live!

Get 1:1 Help Now