Launch console app and wait for exit.

K

Kevin Hodgson

I'm trying to launch a command line utility from my VB.NET App, In my
function I create a process and launch the executable, and need to wait for
it to exit, so that the calling code can move the output file to the
userdata directory, if it's successful.

The problem I'm having is that on myProcess.WaitForExit() I get a
System.NullReferenceException, Object Reference not set to an instance of an
object.

Here's my function.
Private Function GenerateOutput(ByVal myDirectory As DirectoryInfo, ByVal
OptionFile As FileInfo, ByVal LicenceFile As FileInfo) As Boolean
'Spawn Process to execute util and wait for it to finish
Dim psi As New ProcessStartInfo((IO.Path.Combine((myDirectory.ToString),
"util.exe")), "-f " & OptionFile.Name.ToString)
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.CreateNoWindow = True
psi.WorkingDirectory = myDirectory.ToString
psi.UseShellExecute = False
Dim myProcess As Process
Try
myProcess.Start(psi)
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
myProcess.WaitForExit() '**Error occurs on this line**
myProcess = Nothing
Return True
End Function
 
D

David Browne

Kevin Hodgson said:
I'm trying to launch a command line utility from my VB.NET App, In my
function I create a process and launch the executable, and need to wait for
it to exit, so that the calling code can move the output file to the
userdata directory, if it's successful.

The problem I'm having is that on myProcess.WaitForExit() I get a
System.NullReferenceException, Object Reference not set to an instance of an
object.

Here's my function.
Private Function GenerateOutput(ByVal myDirectory As DirectoryInfo, ByVal
OptionFile As FileInfo, ByVal LicenceFile As FileInfo) As Boolean
'Spawn Process to execute util and wait for it to finish
Dim psi As New ProcessStartInfo((IO.Path.Combine((myDirectory.ToString),
"util.exe")), "-f " & OptionFile.Name.ToString)
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.CreateNoWindow = True
psi.WorkingDirectory = myDirectory.ToString
psi.UseShellExecute = False
Dim myProcess As Process
Try
myProcess.Start(psi)
here's your problem. This line should be

myProcess = Process.Start(psi)

myProcess is nothing there and you were never assigning it. Process.Start
is shared so the compiler was letting you run it on a Nothing reference.
But myProcess stayed unassigned. Some other languages won't let you invoke
a shared (static) member through a reference to an instance to avoid such
confusion.

David
 
K

Kevin Hodgson

Thanks, that fixed it.
David Browne said:
here's your problem. This line should be

myProcess = Process.Start(psi)

myProcess is nothing there and you were never assigning it. Process.Start
is shared so the compiler was letting you run it on a Nothing reference.
But myProcess stayed unassigned. Some other languages won't let you invoke
a shared (static) member through a reference to an instance to avoid such
confusion.

David
 
H

Herfried K. Wagner [MVP]

* "Kevin Hodgson said:
I'm trying to launch a command line utility from my VB.NET App, In my
function I create a process and launch the executable, and need to wait for
it to exit, so that the calling code can move the output file to the
userdata directory, if it's successful.

You already got an answer to this question yesterday and confirmed that
the problem was solved...
 
H

Herfried K. Wagner [MVP]

* "Kevin Hodgson said:
No I didn't, you must be thinking of someone else.

Sorry, I thought of someone else. When looking for the old post in OE,
I found your reply in this thread which I didn't see in gnus :-(.
 

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