473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to properly get Handle of started process?

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

Sep 1 '06 #1
4 4341
Hello, Katit!

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

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

KOn next execution I read text file and see if process with same
Kname/handle still runs. However, I see that handle I obtain after
Kexecution 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
Sep 1 '06 #2
K1. Start new process using Process.Start
K2. 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?

Sep 1 '06 #3
Hi Katit

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

Boaz Ben-Porat
Milestone Systems

"Katit" <id*********@gmail.comskrev i en meddelelse
news:11********************@m73g2000cwd.googlegrou ps.com...
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

Sep 1 '06 #4
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

Boaz Ben-Porat wrote:
Hi Katit

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

Boaz Ben-Porat
Milestone Systems
Sep 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ross Bennett | last post by:
Ahoy, Folks! I've been looking all over for this, but I just can't seem to shake any documentation out of the MSDN or from Google. I've reviewed every .NET article on developing Windows...
3
by: David Berman | last post by:
Hi, I've written an application to do a while bunch of Excel automation. I open a file, scan through all the worksheets, extract data, and then I try to close Excel. However, I can see that I'm...
11
by: objectref | last post by:
Hi to all, is there a way to get the window handle of the main window of an application or process ? For example, if someone opens Microsoft Word, he gets a window so he/she can write text....
3
by: Fei Yuan | last post by:
Starting an external process needs to pass it a ProcessStartInfo() object. ProcessStartInfo has a property "UseShellExecute" that can open/print a file. However, I'm having a hard time getting the...
2
by: Fei Yuan | last post by:
Please forgive me re-posting this question since I wasn't clear in my original post. ---- Starting an external process needs to pass it a ProcessStartInfo() object. ProcessStartInfo has a...
10
by: Manfred Braun | last post by:
Hi All, I am writing a proggi, which should monitor some processes. While doing this, I needed a test-program and wrote one, which does nothing else than to consume some cpu, sometimes more,...
10
by: Ricky W. Hunt | last post by:
I have written a close routine to handle an "Exit" button to close the application properly. How do I make sure this gets executed if the user closes it another way (by pressing the "X" in the...
5
by: Jonah Bishop | last post by:
I'm developing a photo album web application for use on a web site, and I'm running into a problem with lengthy operations. My application allows the user to import a number of images at once into...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.