1

Visual Studio 2010 C#

Is it possible to run and close a program from commandline? I need to do something like that:

myProgram.exe -start
-- do something --
myProgram.exe -stop

I know how to implement the first part (String[] args in main) but how can I close my running program?


Edit

myProgram.exe is handling some UDP communication and have to be executed while the stuff in -- do something -- is executed. Sorry for beeing imprecise.

Hint
  • 130
  • 9
  • If you use WinForms this could help: http://stackoverflow.com/questions/4547643/how-can-i-pass-command-line-arguments-to-an-already-running-process – Fox32 Feb 09 '12 at 10:12
  • can u pls provide some more details ? – kbvishnu Feb 09 '12 at 10:13
  • It's difficult to understand what is being asked here: the "`myProgram.exe`" process will exit (cease to exist) once the main function is complete, i.e. after "-- do something --" has completed, there is no need to externally kill it. If this is not what you ment, you need to clarify your question. – Christian.K Feb 09 '12 at 10:15
  • myProgram.exe must run the whole time i am --doing something--. After finishing that stuff (calling other processes) myprogram.exe should be closed. – Hint Feb 09 '12 at 10:20

5 Answers5

1

Is it possible to use taskkill for things like that.

i.e if you want to close myProgram.exe, you can execute

taskkill /IM myProgram.exe
Shai
  • 25,159
  • 9
  • 44
  • 67
  • 2
    also extend this line with `/F` to force close the program. – Pieter888 Feb 09 '12 at 10:14
  • I need to do some stuff on closing the program. Is it possible to register a callback that is triggerd by some kind of closing-event? – Hint Feb 09 '12 at 10:17
1

I'm guessing you know how to inspect the command line arguments and find if stop has been specified? The following should do the job.

        var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
        var matchingProcesses = System.Diagnostics.Process.GetProcesses().Where(x => x.Id != currentProcess.Id && x.ProcessName == currentProcess.ProcessName);
        foreach (var process in matchingProcesses) {
            process.Kill();
        }
Mike Miller
  • 16,195
  • 1
  • 20
  • 27
  • This one is a great approach. Instead of killing the process I am going to send a message to that process. – Hint Feb 09 '12 at 10:34
  • you could also use statics in the application as a crude message system. i.e. killHasBeenSignalled = true and then check for that in the start condition. – Mike Miller Feb 09 '12 at 10:37
  • How can I access a static member of another task? – Hint Feb 09 '12 at 10:38
  • Yes, the static should be shared between any running process in the same app domain (I think). You'll need to consider if using lock is appropriate when accessing it or setting it. – Mike Miller Feb 09 '12 at 10:46
0

Use a command line program that is designed to shut down processes - pskill is one such program.

pskill myProgram.exe

A built in utility is taskkill:

taskkill /im myProgram.exe
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0
  • Get Process Id by name
  • Kill the process
hungryMind
  • 6,931
  • 4
  • 29
  • 45
0

As Fox32 already mentioned in his comment you should really take this approach, cause in that case your application can really shutdown itself gracefully. If you simply go and use taskkill, etc to kill your process from the outer world you could really get into trouble depending of resources you are using (network, database, files, etc.).

Community
  • 1
  • 1
Oliver
  • 43,366
  • 8
  • 94
  • 151