Solved

CreateProcess Equivalent

Posted on 2004-04-19
9
2,286 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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