views:

703

answers:

1

The following code causes a NullReferenceException

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

tStartParameter is:

https://mail.google.com/?view=cm&fs=1&tf=1&[email protected]&su=boogaloo!!

Using the debugger I see that Process.Start is returning null. So.. any thoughts on why this is happening? I'd really like to block program execution until the user is done with the launched process.

UPDATE: Refactoring the code to this:

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

causes this exception:

InvalidOperationException: No process is associated with this object.

+3  A: 

From MSDN, Process.Start will return:

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

In your case, since you're passing a URL to Process.Start rather than an executable, you're not actually kicking off a new process. You're passing the URL to iexplore, or whatever your browser is. And so you get a null back.

In any case, what would it mean to "block program execution until the user is done with the launched process"? Wait until the user closes the web browser? In that case, you may want something like:

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

... which works appropriately for me. It does require you to specify the browser executable, though.

Michael Petrotta
I was hoping to avoid calling iexplore.exe directly.. but it appears I have no choice. Perfect answer. Thanks.
Boo