Click here to Skip to main content
15,887,420 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I am creating an application. Now I need to perform an operation:

Suppose I create a application like name alarm.exe. Now if I am running this application, then I want to terminate all previous process alarm.exe except current process alarm.exe.

Probably it's done with help of process time and date. But I am unable to this code. Please help me to complete this.
Posted
Updated 1-Jul-12 21:34pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Jul-12 3:41am    
Why?
--SA
pradiprenushe 3-Jul-12 7:04am    
You have asked same question two times & also answered both of them by yourself???????????

Rather than that, check if an existing process is running, and if so, terminate yourself.
That way, the process being terminated is in a known state and you can't get data corruption.

One way to do this is here: A simple way to ensure only one instance is running.[^]
 
Share this answer
 
Comments
Member 8454009 3-Jul-12 4:56am    
I couldn't run this code.
The error is " Extension methods must be defined in a non-generic static class ".
The error line of code is: " public partial class Form1 : Form ".
Probably you get which line I mean.
OriginalGriff 3-Jul-12 5:03am    
The error is pretty explicit: you must define a separate static class to hold extension methods.
public static class ExtensionMethods
{
public static MyMethod(this MyClass value...
}
This is generic to all extension methods, not just ones I write! :laugh:
Actually, this is a pretty bad idea. The right idea is this: develop this application the way it could get request from the second instance to change its behavior and assume the responsibility of the second instance. The best method of communications I know from all the different methods I knew is classic .NET remoting.

Some instance of the application process tries to connect to the remoting service as a client. It this attempt fails, this instance is the only one, so it start playing the role of the remoring service. When some client is connected, it should accept the data sent to the second instance of the process and continue running with "new mission". After the second process passes data to the first one, this second process terminate itself. This is always better, and the termination of a remote process is careless termination, always bad and generally unsafe.

This way, there is no need to terminate the process, which is not a nice thing. However, it you are so stubborn to deny my good idea (which was actually proven to be very productive), you can use System.Diagnostics.Process.Kill.

But listen, why would you want all that? Anyway, to create an instance of the Process class for an external process, you will need to know its id to be able to call the factory method Process.GetProcessById:
http://msdn.microsoft.com/en-us/library/76fkb36k.aspx[^].

Better don't even think about getting all processes (with Process.GetProcesses(string)) by name. How would you guarantee that this name is unique? So, you will need some communication between the instances of your processes anyway. It could be, say, shared memory, or something else, like the same very remoting. But it you already have communication between your processes, why would you use it for killing, if you can simply use my schema, when a new process terminates itself (instead of terminating the older one) simply by returning from the entry point method? Whatever you do, this method will always be better.

—SA
 
Share this answer
 
Terminate all previous running process except current running process of an same application:

I am using the following code to solve this problem.

Process CurrentProcess = Process.GetCurrentProcess();

                foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
                {
                    if (p.Id != CurrentProcess.Id)
                    {
                        if (p.ProcessName == "Alarm")
                        {
                            p.Kill();
                        }
                    }
                }
 
Share this answer
 

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