Solved

CreateProcess Equivalent

Posted on 2004-04-19
9
2,308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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