Process.WaitForExit () throws a NullReferenceException

The following code throws a NullReferenceException

tStartParameter = String.Format(tStartParameter, tTo, tSubject)
tProcess = Process.Start(New ProcessStartInfo(tStartParameter) _
           With {.UseShellExecute = True})
tProcess.WaitForExit()

      

tStartParameter:

https://mail.google.com/?view=cm&fs=1&tf=1&to=t@example.com&su=boogaloo!!

      

Using the debugger, I can see that Process.Start is returning null. So ... any thoughts on why this is happening? I would really like to block the execution of the program until the user has executed the running process.

UPDATE: Refactoring the code:

tStartParameter = String.Format(tStartParameter, tTo, tSubject)
tProcess = New Process
tProcess.StartInfo = New ProcessStartInfo(tStartParameter) _
                     With {.UseShellExecute = True}
tProcess.Start()
tProcess.WaitForExit()

      

raises this exception:

InvalidOperationException: No process is associated with this object.

0


a source to share


1 answer


Process.Start is returned from MSDN:

A new process component that is associated with a process resource, or a null reference (Nothing in Visual Basic) if there is no process resource started (for example, if an existing process is reapplied).

In your case, since you are passing the URL to Process.Start and not the executable, you are not actually starting a new process. You are passing the URL to iexplore or whatever your browser. And you will get a null value.



In any case, what would it mean to "block the execution of the program until the user has executed the running process"? Wait until the user closes the web browser? In this case, you might need something like:

Process p = Process.Start("iexplore", "http://www.google.com");
p.WaitForExit();

      

... which works for me. However, this requires specifying the browser executable.

+4


a source







All Articles