Sending Ctrl+C to Apache

C

cartoper

I am in a pickel, I MUST start and stop the apache web server in
console mode. The only way to shut down apache cleaning is by
pressing <Ctrl>+C. I have tried everything I know and I cannot
duplicate that in C# code. This is what I have, which is NOT working:

[DllImport("Kernel32.dll")]
public static extern bool GenerateConsoleCtrlEvent(int ctrlEvent, int
processGroupId);
[DllImport("Kernel32.dll")]
public static extern int GetProcessId(IntPtr Process);

int processID = GetProcessId(process.Handle);
GenerateConsoleCtrlEvent(0, processID);

Of course, process IS the apache service the application started.
Anyone have any more thoughts?
 
G

Guest

Have you tried:
System.Windows.Forms.SendKeys?

eg:
IWshRuntimeLibrary.WshShell Shell;

Shell.Run("calc");
Threading.Thread.Sleep(100);
Shell.AppActivate("Calculator");
Shell.SendKeys("101")
Sleep(500);
Shell.SendKeys("*");



or you could show it some tough love:
Process[] Processes = Process.GetProcessesByName("apache");
foreach( Process myProcess in Processes)
{
myProcess.CloseMainWindow(); //not so tough...
myProcess.Kill(); //Oh dear...... You can just imagin what this does.

}

I dont think PInvokes are required, but let me know how it goes :)
 
H

herc

Have you tried:
System.Windows.Forms.SendKeys?

eg:
IWshRuntimeLibrary.WshShell Shell;

Shell.Run("calc");
Threading.Thread.Sleep(100);
Shell.AppActivate("Calculator");
Shell.SendKeys("101")
Sleep(500);
Shell.SendKeys("*");

or you could show it some tough love:
Process[] Processes = Process.GetProcessesByName("apache");
foreach( Process myProcess in Processes)
{
myProcess.CloseMainWindow(); //not so tough...
myProcess.Kill(); //Oh dear...... You can just imagin what this does.

}

I dont think PInvokes are required, but let me know how it goes :)

I was doing the last thing you suggested, already, that is getting the
process by name and killing them all. The downside to that was that I
also had a develpment instance running as a service and it, too, was
killed.

What I had missed what the CloseMainWindow() call, this works like a
charm! The whole deal is that when you start one instance of apache,
it starts one or more children. When you Kill() the process you
started, the children keep running. But CloseMainWindow() sends the
right info to the parent apache and it closes the other servers
gracefully!

Thanks!
 
G

Guest

You were trying to kill children eh? LOL !!!

Anyhoo.....
Glad to be of some assistance.

herc said:
Have you tried:
System.Windows.Forms.SendKeys?

eg:
IWshRuntimeLibrary.WshShell Shell;

Shell.Run("calc");
Threading.Thread.Sleep(100);
Shell.AppActivate("Calculator");
Shell.SendKeys("101")
Sleep(500);
Shell.SendKeys("*");

or you could show it some tough love:
Process[] Processes = Process.GetProcessesByName("apache");
foreach( Process myProcess in Processes)
{
myProcess.CloseMainWindow(); //not so tough...
myProcess.Kill(); //Oh dear...... You can just imagin what this does.

}

I dont think PInvokes are required, but let me know how it goes :)

I was doing the last thing you suggested, already, that is getting the
process by name and killing them all. The downside to that was that I
also had a develpment instance running as a service and it, too, was
killed.

What I had missed what the CloseMainWindow() call, this works like a
charm! The whole deal is that when you start one instance of apache,
it starts one or more children. When you Kill() the process you
started, the children keep running. But CloseMainWindow() sends the
right info to the parent apache and it closes the other servers
gracefully!

Thanks!
 
H

herc

Actually, I ran into another interesting issue. When I launch apache
and tell it to hide the process window, CloseMainWindow() does nothing:
( I am assuming it is because there is no main window to close. Oh
well, I will simply minimize the window for early testing and before
release I will fix apache! The docs claim that you can lanuch another
instance to kill the one running, but it doesn't work, I will change
that! hehehehe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top