Solved

how to recognize that process is running in c #

Posted on 2009-04-06
8
245 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

744 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

13 Experts available now in Live!

Get 1:1 Help Now