Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CreateProcess Equivalent

Posted on 2004-04-19
9
Medium Priority
?
2,311 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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