Link to home
Start Free TrialLog in
Avatar of tbbrhun
tbbrhun

asked on

VB.NET: Cannot convert Sender object to Process object. DirectCast and CType fail

Hello:

I'm new to VB.NET, and I have the following problem: I'm trying to start an external application from a VB.NET program, let's say it's Notepad.exe, and take an action when the external application closes:

I have the following code snippets:

'Form-level declaration
Private WithEvents P As Process

'This procedure works fine, and launches the Notepad without problem
Private Sub btnStartProcess_Click...
     P = New Process()
     AddHandler P.Exited, AddressOf P_Exited
     
     With P
          .EnableRaisingEvents = True
          .SynchronizingObject = Me
          .StartInfo.FileName = "C:\Notepad.exe"
          .Start()
     End With
End sub

'This is the event that fires when Notepad closes
Friend Sub P_Exited(ByVal Sender As Object, ByVal e As System.EventArgs)
     Dim MyProcess As Process
     Try    
          'This is the line that cuases the problem
          MyProcess = DirectCast(Sender, Peocess)
     Catch Exc As Exception
          Messagebox.Show(Exc.ToString)
     Finally
          MyProcess.Close()
     End Try
End Sub

When the code in the P_Exited procedure runs, the problem line throws the exception #13 'Specified Cast is not valid'. If Process object is the one that raised this event, why Sender (As Object) cannot be converted to Process object?

I also was trying CType instead of DirectCast with the same result.

Thanks!
Avatar of tpatten
tpatten

Is this the exact code, cut and pasted?

MyProcess = DirectCast(Sender, Peocess)

If so, Process is spelled wrong. I'm more familiar with C#, but can you do this: (Process)sender to cast?

Do you have "Option Strict On" set? Just curious.

Avatar of tbbrhun

ASKER

Thanks.
Don't mind the spelling, I was retyping when posting this, My code compiles with no problem, there are no spelling errors in the actual code, no underlined words etc.
Option Strict was off by default. Obviously, it didn't help when I set it on.
Re: (Process)sender to cast - I'm not sure I understand this, sorry. I never programmed in any of the C languages. I came to VB from DOS BASIC.
Thanks
I made a small experiement, changing your P_Exited function:

Sub P_Exited(ByVal Sender As Object, ByVal e As System.EventArgs)
    MessageBox.Show(Sender.GetType.ToString)
End Sub

and found that sender is actually the Form class. Obviously, it cannot be casted to Processs.
If you want to get access to the process launched in btnStartProcess_Click, make Process instance class member and use it in P_Exited. Pay attention that process itself is not existing at this time.

From MSDN topic "Process.Exited Event":
Because the associated process has exited, the Handle property of the component no longer points to an existing process resource. Instead, it can be used only to access the operating system's information about the process resource. The system is aware of handles to exited processes that have not been released by Process components, so it keeps the ExitTime and Handle property information in memory until the Process component specifically frees the resources.

Note: Even if you have a handle to an exited process, you cannot call Start again to reconnect to the same process. Calling Start automatically releases the associated process and connects to a process with the same file but an entirely new Handle.
Avatar of tbbrhun

ASKER

Dear AlexFM:
Thanks for this comment. At this point I feel I have to give some more info. I also went through the exactly same experiement to find out the the sender is actually a form object. I just didn't realize why. The problem with using a class-level Process object inside the P_Exited event procedure is that I'm trying to launch several processes at the smae time (which I didn't mention to simplify the problem), and P_Exited event procedure was supposed to handle Exited events that migh come from any of the several Process objects I might have. I just wanted to know which process fired the event, and retreive the name of the application that closed.
If you know a solution to this, I'll gladly welcome it, if not, I'll accept you previous comment as an answer
Thanks and regards. Brian






ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tbbrhun

ASKER

AlexFM:

Thanks a lot for your help. If by a chance you have an idea of how else I can do it, please let me know.

Thanks again!