?
Solved

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

Posted on 2003-03-15
6
Medium Priority
?
1,319 Views
Last Modified: 2017-03-02
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!
0
Comment
Question by:tbbrhun
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:tpatten
ID: 8145340
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.

0
 

Author Comment

by:tbbrhun
ID: 8145438
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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8146173
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.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 

Author Comment

by:tbbrhun
ID: 8146689
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






0
 
LVL 48

Accepted Solution

by:
AlexFM earned 80 total points
ID: 8149651
I don't see a way to do this. Possible reason is, that on the low level, having a handle to the process which is already terminated, we cannot get any information from it, because all process resources are already released.
0
 

Author Comment

by:tbbrhun
ID: 8149768
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!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question