.NETGURU
C# unable to access property "exittime" on terminated process
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.

Post a new message to this list...

Dave
I have the following code in a c# project of mine.

public void SetProcess(Process NewProcess)
{
    CurProcess = NewProcess;

    CurProcess.WaitForExit();

    MessageBox.Show(CurProcess.ExitTime.ToString());
}

Everything goes well until the last line. The process waits to exit,
then immediately after, I need to get the ExitTime. When I try to do
this in the immediate window here are my results:

?CurProcess.ExitTime
<error: an exception of type: {System.InvalidOperationException}
occurred>

If I just let it run, I get an "Unhandled Exception" Error message.
Here is the first few lines of info from that message.

"************** Exception Text **************
System.InvalidOperationException: Process was not started by this
object, so requested information cannot be determined.
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.get_ExitTime()"

I can get information on any other property I have tried. Even
HasExited returns true! Can anyone tell me something I'm doing wrong?
I have looked in MSDN help and on these groups for the answer to no
avail.

Thanks

-Dave
Reply to this message...
 
    
Dave
Ok guys, I think I figured out my problem and I even have a fix for
it. Here is the problem (I think).

I can't get the exittime on something I didn't personally create
through code. I'm getting handles to processes that are running, but
not ones that I created. So I'm thinking that's why I can't get the
exittime.

Hold on though, I'm not done yet ;).

I still need to get the time the process exited, I could do that by
using the code I posted above

public void SetProcess(Process NewProcess)
{
    GameProcess = NewProcess;

    GameProcess.WaitForExit();
                
    //Grab DateTime.Now() here.
}

and then just grab the current DateTime once that line of code is
passed, but as soon as my app encounters ".WaitForExit();", I get a
busy hourglass and my app stops responding until the process exits.
That won't work for what I'm doing, and I don't want to multithread
this. It needs to have as small a footprint as possible.

Enter the Exited event handler. I found that the Process class has an
Exited event handler that fires when the process terminates. Now, I am
new to c# and don't know how to write this event handler
syntactically. I know that the following doesn't work:

private void GameProcess_Exited(System.EventArgs EventArgs)
{
            
MessageBox.Show("Working");

}

According to MSDN help the handler is supposed to take an argument of
type System.EventArgs, but I don't know what else I might have typed
incorrectly. I know in VB6 it was very particular about you getting
the return type and all arguments exactly correct. Also an unknown for
me is in VB6 you would have to dim WithEvents this variable to get it
to fire any events. Do I have to do this in C# too?

Any help much appreciated.
Reply to this message...
 
    
Dave
Found the fix! Here it is:

Here's some code you have to do when you first grab a
handle to an instance of a process:

public void SetProcess(Process NewProcess)
{
MyProcess = NewProcess;
            
// Handle the Exited event that the Process class fires.
this.MyProcess.Exited += new System.EventHandle(this.MyProcess_Exited);

MyProcess.EnableRaisingEvents = true;
// The code I found also said to put the following line in, but
// my code wouldn't compile with it. Oh well, didn't need it
// in my case.
//GameProcess.SynchronizingObject = this;

}

Here's the EventHandler code:

private void MyProcess_Exited(object sender, System.EventArgs e)
{    
MessageBox.Show("Working");
}

This code is awesome it works great!
Reply to this message...
 
    
Robert Jordan
[Original message clipped]

private void GameProcess_Exited(object sender, EventArgs e)
{
MessageBox.Show("Working");
}

bye
Rob
Reply to this message...
 
 
System.DateTime
System.Diagnostics.Process
System.EventArgs
System.EventHandler
System.InvalidOperationException
System.Windows.Forms.MessageBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification