Not getting process state

  • Thread starter Ashutosh Bhawasinka
  • Start date
A

Ashutosh Bhawasinka

Hi,
I am starting a process and I need to monitor it (wait and check if its
still responding).

But, I it seems that the Process.HasExited or Process.Exited doesn't work.

I just need to know if the process is running & responding.

I am using

void somefn(...)
{
if(...)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = this.Path;
psi.Arguments = this.CreateArguments();
psi.ErrorDialog = false;
psi.UseShellExecute = true; //tried with false also
Process p = Process.Start(psi);

p.Exited += new EventHandler(Exited);
while (p.HasExited)
{
Thread.Sleep(1000);
}
}
return Success;
}

void Exited(object sender, EventArgs e)
{
MessageBox.Show("Done");
}

The execution never enters the while loop nor the Exited method is called.

Any ideas???

Regards,
Ashutosh
 
G

Gilles Kohl [MVP]

Hi,
I am starting a process and I need to monitor it (wait and check if its
still responding).

But, I it seems that the Process.HasExited or Process.Exited doesn't work.

I just need to know if the process is running & responding.

I am using

void somefn(...)
{
if(...)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = this.Path;
psi.Arguments = this.CreateArguments();
psi.ErrorDialog = false;
psi.UseShellExecute = true; //tried with false also
Process p = Process.Start(psi);

p.Exited += new EventHandler(Exited);

Hmm, try swapping the two lines above ...
while (p.HasExited)

Don't you mean while(!p.HasExited)?

{
Thread.Sleep(1000);
}
}
return Success;
}

void Exited(object sender, EventArgs e)
{
MessageBox.Show("Done");
}

The execution never enters the while loop nor the Exited method is called.

Any ideas???

See above ...

Regards,
Gilles.
 
A

Ashutosh Bhawasinka

Hi,
Thanks for pointing out the mistake....it's such a silly one :)

Actually, I was more interested in the callback. Do, you have any clue
on that??

Regards,
Ashutosh

Gilles Kohl [MVP] wrote:.
 
P

parez

Hi,
Thanks for pointing out the mistake....it's such a silly one :)

Actually, I was more interested in the callback. Do, you have any clue
on that??

Regards,
Ashutosh

Gilles Kohl [MVP] wrote:.

That mite fix your problem. GC was probably taking your object
(process) since you were not waiting in that loop and that might be
the reason why Exited never got called..
 
G

Gilles Kohl [MVP]

Thanks for pointing out the mistake....it's such a silly one :)
Actually, I was more interested in the callback. Do, you have any clue
on that??

Try adding

p.EnableRaisingEvents = true;

Regards,
Gilles.
 
L

Linda Liu[MSFT]

Thanks all who help on this issue!

Hi Ashutosh,

In summary, there're three problems in your sample code snippet.

1. You should set the EnableRaisingEvents property of the Process to true
for the Process component to receive notification that the process has
exited.

2. You should subscribe the Exited event of the Process before call the
Start method of the Process.

3. Change the condition in the while statement to "!p.HasExited" within the
"somefn" method.

For more information about the Process's Exited event, please refer to the
following MSDN document:
https://msdn2.microsoft.com/en-us/library/system.diagnostics.process.exited.
aspx

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Ashutosh Bhawasinka

Thanks everyone for your help. The issue is resolved now.

Looks like I was sleeping while writing that code :)

Regards,
Ashutosh
 

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