Solved

CreateProcess Equivalent

Posted on 2004-04-19
9
2,282 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
Comment Utility
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
Comment Utility
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_
Comment Utility



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
Comment Utility
.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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

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

Expert Comment

by:tgannetts
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great.  Tom, thanks for your help.

MT
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

12 Experts available now in Live!

Get 1:1 Help Now