Solved

CreateProcess Equivalent

Posted on 2004-04-19
9
2,285 Views
Last Modified: 2010-10-05
Can't find a clear answer on this.

I'm very familiar with CreateProcess the from VC++ angle, from which I'm able to start a process in a hidden window with a handle so that I can close it down at a later time.  I need to accomplish the same thing with C#.

There are several examples on this site and others that show how to start an executable with CreateProcess and Process, etc., but they do not complete the 'loop'.

I need a specific example that gets a handle to the process (executable) in a hidden child window so that I may shut it down later - not just an example that starts the target executable.

Good points for a good, fast solution.  

Thanks,

MT
0
Comment
Question by:supertedusa
  • 4
  • 4
9 Comments
 
LVL 5

Expert Comment

by:tgannetts
ID: 10860204
Is this what you were looking for:

using System.Diagnostics;

...

//Create new Notepad application in hidden window
Process proc = new Process();
proc.StartInfo.FileName = "Notepad.exe";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

//Start Process
proc.Start();

//Store Unique Process ID for later use
int hwnd = proc.Id;

...

//Get Process from ID
Process killproc = new Process();
killproc = Process.GetProcessById(hwnd);

//Kill Process
killproc.Kill();

Tom.
0
 

Author Comment

by:supertedusa
ID: 10860233
Does .Kill() allow the app to shutdown gracefully on it's own?  Like PostMessage()?  I want to send the app a message which allows this.

Thanks,

MT
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10860274



no... process.Kill() is like killing the process from a process monitor.  It stops the process no matter what.

process.close() will close the process gracefully and free up resources.

process.Responding() returns a true or false if the program is still responsive or not
0
 
LVL 5

Expert Comment

by:tgannetts
ID: 10860292
.Kill() immediately terminates the process. Close() is also available which sends a message to the application instructing it to shut down, although how 'gracefully' depends on the application you are attempting to close.

Tom
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:supertedusa
ID: 10860354
So do I use proc.WaitForExit() somehow to wait on the thing to close?
0
 
LVL 5

Expert Comment

by:tgannetts
ID: 10860459
You can use WaitForExit to wait for the process to end. You can pass a timeout figure as a parameter to wait a certain amount of time and then check the HasExited property to check if the process terminated during this period, or you can wait indefinitely.

As  TAD  has said, you can check the process.Responding value to check if the process has hung during the termination process, and if required action the Kill Method.

In addition to all this, an event can be set to fire when a Process exits (Process.Exited). For this to be raised, the EnableRaisingEvents flag must be set to true when the Process is started.

Tom
0
 

Author Comment

by:supertedusa
ID: 10860572
Do I implement this in a VC++ style?  Like

WaitForSingleObject(piMyProc.hProcess, INFINITE);      

?

Please post some sample code if you have it.

Thanks,

MT
0
 
LVL 5

Accepted Solution

by:
tgannetts earned 300 total points
ID: 10861053
For example, the following code will attempt to close a Process, wait 30 seconds for it to terminate, check to see if it has terminated after that time period, and kill the process if necessary:

//Set timeout limit
int timeout = 30000;

//Get Process that matches ID
Process killproc = new Process();
killproc = Process.GetProcessById(hwnd);

//Send Close message
killproc.Close();

//Wait for process to exit
killproc.WaitForExit(timeout);

//Check process has exited
if (killproc.HasExited = false)
{
     killproc.Kill;
}

To wait indefinitely, either exclude the timeout parameter or set it to int.MaxValue. (The compiler assumes this value to represent infinity)

Tom.
0
 

Author Comment

by:supertedusa
ID: 10861101
Great.  Tom, thanks for your help.

MT
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# string handling 1 27
Resolve Dependency Issues 4 44
Two different visual studio versions 3 22
Close form "before" open 3 23
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

23 Experts available now in Live!

Get 1:1 Help Now