?
Solved

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

Posted on 2003-03-15
6
Medium Priority
?
1,309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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