BUG? : Process.Exited event not working

  • Thread starter Thread starter Sunny
  • Start date Start date
S

Sunny

Hi,
can someone confirm that this code is not working, and is there a reason
for this? (yes, I know for WaitForExit, this is just a sample, which
demonstrates that the event is not fired).

And ... the strange thing is, that if I uncomment the 3
Console.WriteLine statements which are before WaitOne statement, it
works as expected. But if I put comment on just one of them, the event
is not fired.

I'm using VS.Net 2003, framework 1.1, Win2K

Cheers
Sunny




using System;
using System.Diagnostics;
using System.Threading;

namespace signall
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Process getpub = new Process();
getpub.StartInfo =
new ProcessStartInfo(@"c:\winnt\system32\ipconfig.exe");
getpub.StartInfo.CreateNoWindow = true;
getpub.Exited += new EventHandler(getpub_Exited);
getpub.StartInfo.UseShellExecute = false;
getpub.StartInfo.RedirectStandardOutput = true;
getpub.StartInfo.RedirectStandardError = true;
getpub.Start();
//Console.WriteLine(getpub.StandardOutput.ReadToEnd
());
//Console.WriteLine(getpub.StandardError.ReadToEnd
());
//Console.WriteLine(getpub.HasExited.ToString());

processWait.WaitOne();

Console.WriteLine(getpub.ExitCode);
Console.WriteLine(getpub.StandardOutput.ReadToEnd
());
Console.ReadLine();
}

static AutoResetEvent processWait = new AutoResetEvent
(false);


private static void getpub_Exited(object sender, EventArgs
e)
{
Console.WriteLine("Process ended.");
processWait.Set();
}
}
}
 
Set the 'EnableRaisingEvents' property of the process object to true.
Dunno why it fires the event when you read out the HasExited property.

Niki
 
Thanks Niki,

The strange thing is, that HasExited is not enough. One have to call all
3 properties to fire the event.

Thanks for the help

Sunny

Set the 'EnableRaisingEvents' property of the process object to true.
Dunno why it fires the event when you read out the HasExited property.

Niki

Sunny said:
Hi,
can someone confirm that this code is not working, and is there a reason
for this? (yes, I know for WaitForExit, this is just a sample, which
demonstrates that the event is not fired).

And ... the strange thing is, that if I uncomment the 3
Console.WriteLine statements which are before WaitOne statement, it
works as expected. But if I put comment on just one of them, the event
is not fired.

I'm using VS.Net 2003, framework 1.1, Win2K

Cheers
Sunny




using System;
using System.Diagnostics;
using System.Threading;

namespace signall
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Process getpub = new Process();
getpub.StartInfo =
new ProcessStartInfo(@"c:\winnt\system32\ipconfig.exe");
getpub.StartInfo.CreateNoWindow = true;
getpub.Exited += new EventHandler(getpub_Exited);
getpub.StartInfo.UseShellExecute = false;
getpub.StartInfo.RedirectStandardOutput = true;
getpub.StartInfo.RedirectStandardError = true;
getpub.Start();
//Console.WriteLine(getpub.StandardOutput.ReadToEnd
());
//Console.WriteLine(getpub.StandardError.ReadToEnd
());
//Console.WriteLine(getpub.HasExited.ToString());

processWait.WaitOne();

Console.WriteLine(getpub.ExitCode);
Console.WriteLine(getpub.StandardOutput.ReadToEnd
());
Console.ReadLine();
}

static AutoResetEvent processWait = new AutoResetEvent
(false);


private static void getpub_Exited(object sender, EventArgs
e)
{
Console.WriteLine("Process ended.");
processWait.Set();
}
}
}
 
Sunny said:
Thanks Niki,

The strange thing is, that HasExited is not enough. One have to call all
3 properties to fire the event.

Really? On my PC, HasExited is enough!

This is the ROTOR source of the HasExited property:

public bool HasExited {
get {
if (!exited) {
EnsureState(State.Associated);
IntPtr processHandle = (IntPtr)0;
try {
processHandle = OpenProcessHandle();
if (processHandle ==
NativeMethods.INVALID_HANDLE_VALUE) {
exited = true;
}
else {
int exitCode;
if
(!NativeMethods.GetExitCodeProcess(processHandle, out exitCode))
throw new Win32Exception();
if (exitCode != NativeMethods.STILL_ACTIVE) {
this.exited = true;
this.exitCode = exitCode;
}
}
}
finally {
ReleaseProcessHandle(processHandle);
}
if (exited)
RaiseOnExited();
}
return exited;
}
}

See the "RaiseOnExited" call in the end? I think this should always raise
the event...
I'm not sure why they built it that way, but if you set
"EnableRaisingEvents" - everything works fine anyway, so who cares?

Niki
 
Thanks Niki


Sunny

Really? On my PC, HasExited is enough!

This is the ROTOR source of the HasExited property:

public bool HasExited {
get {
if (!exited) {
EnsureState(State.Associated);
IntPtr processHandle = (IntPtr)0;
try {
processHandle = OpenProcessHandle();
if (processHandle ==
NativeMethods.INVALID_HANDLE_VALUE) {
exited = true;
}
else {
int exitCode;
if
(!NativeMethods.GetExitCodeProcess(processHandle, out exitCode))
throw new Win32Exception();
if (exitCode != NativeMethods.STILL_ACTIVE) {
this.exited = true;
this.exitCode = exitCode;
}
}
}
finally {
ReleaseProcessHandle(processHandle);
}
if (exited)
RaiseOnExited();
}
return exited;
}
}

See the "RaiseOnExited" call in the end? I think this should always raise
the event...
I'm not sure why they built it that way, but if you set
"EnableRaisingEvents" - everything works fine anyway, so who cares?

Niki
 
Back
Top