About System.Process of .NET

B

Benny Lam

Hi,

I have a question about System.Process of .NET in C#...

in this code:

static void Main(string[] args)
{
string filename = args[0];

Process p = new Process();
p.StartInfo.FileName =filename;
p.StartInfo.UseShellExecute = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
p.WaitForExit();
Console.WriteLine("Bye");
}

When i use it to open file, e.g. test.exe c:\123.xls and text.exe c:\456.xls.

the second text.exe will show the error.

that means Process can not monitor the same application at the same time.
Right?

Anyone knows how to solve it?
 
B

Benny Lam

Hi Wilson,

It shows the p.WaitForExit has error, "No process is associated with this
object."

I'm doing the test during two "cmd.exe"...

Phil Wilson said:
You'll need to be more explicit. What is this error that you're referring
to?
--
Phil Wilson
[MVP Windows Installer]

Benny Lam said:
Hi,

I have a question about System.Process of .NET in C#...

in this code:

static void Main(string[] args)
{
string filename = args[0];

Process p = new Process();
p.StartInfo.FileName =filename;
p.StartInfo.UseShellExecute = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
p.WaitForExit();
Console.WriteLine("Bye");
}

When i use it to open file, e.g. test.exe c:\123.xls and text.exe
c:\456.xls.

the second text.exe will show the error.

that means Process can not monitor the same application at the same time.
Right?

Anyone knows how to solve it?
 
F

Family Tree Mike

Benny Lam said:
Hi Wilson,

It shows the p.WaitForExit has error, "No process is associated with this
object."

I'm doing the test during two "cmd.exe"...


Are you seeing both spreadsheets shown in one instance of Excel? That is
what I would expect.
 
B

Benny Lam

yes, i think so.

But it's not using different thread to monitor it? Because I think it should
be monitoring per session.

um... anything i can solve it?
 
F

Family Tree Mike

If you are really after working with MS office objects, then I would use
office interop objects rather than processes. By connecting to an
Excel.Application object, you should be able to monitor when each sheet is
closed, edited, printed, or anything else. With a singleton application,
like Excel, I'm afraid you cannot get to what I believe you are seeking.

In general though, your code would have worked. Take notepad for example.
It would have opened two instances of itself, each with the provided file.
 
B

Benny Lam

Sigh.... I may need to deeply consider your suggestion... Many thanks!

But, i still want to hear other comments...
 
P

Patrice

I noticed you taked about cmd.exe ? What if you launch the real exe file
instead ?

If you are using something like start or cmd myfile.ext, I'm afraid it will
launch the application associated with the "ext" extension and then will
terminate. So basically you are not monitoring the actual application but
the application that launches the actual application...

I would do a quick test using notepad.exe as suggested by Mike as a proof of
concept (to see if it works when launching notepad.exe and to see if it
still works if using cmd.exe to open a text file that is launching
notepad.exe indirectly still works or make what previously worked fails)...
 
B

Benny Lam

Oh... i tried to run it alone, it's still the same result.

I may try to monitor the process name or whatever, not use the WaitForExit
again...

Thanks your hlep.
 

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