Solved

how to recognize that process is running in c #

Posted on 2009-04-06
8
252 Views
Last Modified: 2013-12-17
I am coding with C# and need to know how to prevent my new.exe executable from running if my "current.exe" process is currently running.  For instance, if current.exe is running and I click on my new.exe to run an info message displays and informs that current.exe is currently running, if it is not currently running my new.exe launches.

I know I have to do an if event:

if {current.exe is running} display message
else open up new.exe or return;

thank you
0
Comment
Question by:systems_ax
8 Comments
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079112
About 6 lines of code,

here is how to Get a List of Running Processes in C#:
http://www.howtogeek.com/howto/programming/get-a-list-of-running-processes-in-c/

How to get the Process Name in C#
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=17937

C# : Get the all running processes and applications
http://www.eggheadcafe.com/tutorials/aspnet/cb972d77-0892-4bf7-834d-c23b6dd5c03a/c--get-the-all-running.aspx
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24079124
Try this:
    using System.Diagnostics;
 
    Process aProcess = Process.GetCurrentProcess();
    string aProcName = aProcess.ProcessName;
			
    if (Process.GetProcessesByName(aProcName).Length > 1)
    {
        Application.ExitThread();
    }

Open in new window

0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24079199
System.Diagnostics.Process.GetProcessesByName("Current")
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:systems_ax
ID: 24079380
thanks,
I have the following anf nothing happens when I run outlook and click on the button that contains the following code: any ideas???

private void button4_Click(object sender, EventArgs e)
        {
            Process OUTLOOK= Process.GetCurrentProcess();
            string aProcName = OUTLOOK.ProcessName;

            if (Process.GetProcessesByName("OUTLOOK.exe").Length > 1)
            {
                MessageBox.Show("Close OUTLOOK first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

0
 
LVL 1

Author Comment

by:systems_ax
ID: 24079481
i fixed it,works great, thanks so much
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24079536
Great! Can you post the fix to your code, for the sake of the solution for others? Thanks.
0
 
LVL 1

Author Comment

by:systems_ax
ID: 24080323
sure, it is:

private void button4_Click(object sender, EventArgs e)
        {
            Process OUTLOOK= Process.GetCurrentProcess();
            string aProcName = OUTLOOK.ProcessName;

            if (Process.GetProcessesByName("OUTLOOK").Length > 0)
            {
                MessageBox.Show("Close OUTLOOK first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
0
 
LVL 1

Author Comment

by:systems_ax
ID: 24080329
i'll be posting another ? about another function I am not familiar with
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Signal R -send data to client: only if they are allowed to see it ? 1 53
Cant save 3D 4 21
HttpPostedFile 1 26
FTP file download using c# 3 23
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

821 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