Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I just created two console programs in c# named console1 and console2.Now,when i run the console1 it have to check whether the console2 is currently running or not?Kindly tell me the code.

Thanks
Posted
Comments
Xeshan Ahmed 14-Oct-11 9:12am    
follow the shortest method call
Xeshan Ahmed 14-Oct-11 9:12am    
added in solution two
Xeshan Ahmed 14-Oct-11 9:12am    
because LINQ is even faster then foreach loop

use the following method
C#
private bool IsAlreadyRunning(string ExecutableFileName)
{
return Process.GetProcesses().ToList().Where(obj=>obj.ProcessName==ExecutableFileName).ToList().Count==0?false:true;
}


if you want to find console1 simply call
C#
bool bac=IsAlreadyRunning("console1");
 
Share this answer
 
Use the Process class:
C#
private static bool IsRunning(string name)
    {
    Process[] processes = Process.GetProcesses();

    foreach (Process p in processes)
        {
        if (p.ProcessName == name)
            {
            return true;
            }
        }
    return false;
    }
 
Share this answer
 
Comments
Xeshan Ahmed 14-Oct-11 9:09am    
quite lengthy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900