How to properly get Handle of started process?

K

Katit

Here is what I need to accomplish:

1. Start new process using Process.Start
2. Get it's handle and save to text file.

On next execution I read text file and see if process with same
name/handle still runs. However, I see that handle I obtain after
execution does not match handle that I get from list.

My code like this:

Process myGen = new Process();
myGen.StartInfo.FileName = Environment.CurrentDirectory +
"\\SampleGenerator.exe"; myGen.Start();
Thread.Sleep(1000); // I had to do this in order to avoid "Unable to
enumerate process
//modules" exception. Is there solution to this problem?

GeneratorProcess gp2 = new GeneratorProcess();
gp2.Name = myGen.ProcessName;
gp2.Handle = myGen.Handle.ToInt32(); //I get handle from process object
that I started

Process runs for over minute. When I execute code to enumerate
processes
Process[] RunningProcesses = Process.GetProcesses();

I get this process with completely different Handle.
What am I doing wrong?

TIA
 
V

Vadym Stetsyak

Hello, Katit!

K> 1. Start new process using Process.Start
K> 2. Get it's handle and save to text file.

This doesn't have sense, since on the next process start handle will change.

K> On next execution I read text file and see if process with same
K> name/handle still runs. However, I see that handle I obtain after
K> execution does not match handle that I get from list.

You observer this because there is no guarantee that OS will reuse process handles.

What are you trying to accomplish?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
K

Katit

K> 1. Start new process using Process.Start
K> 2. Get it's handle and save to text file.

This doesn't have sense, since on the next process start handle will change.

I'm executing outside program from my app and want to grab it's handle
which I save.
Then next time my program executes (it run's on a schedule) I read list
of system processes and compare handles to stored ones to see if it was
process executed by me.

It seem to be working now if I use Process.Id..
You observer this because there is no guarantee that OS will reuse process handles.
See above


Also, do you know why I get exception(see in comment in original post)
when try to read Process properties immediately after Process.Start?
 
B

Boaz Ben-Porat

Hi Katit

Have you tried to use Process.Id instead of Process.Handle ?

Boaz Ben-Porat
Milestone Systems
 
K

Katit

Id works for that purpose.

But I still have annoying problem.

If I try to get Id next line after Process.Start - I get exception
"unable to enumerate process modules"

This is some kind of timing issue. Thread.Sleep(xxx) fixes it, but it's
ugly. And of course it works when you step through code.

Any way to do callback when process started or something like this that
will be more reliable?

Thanks!
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top