« Hyperlinks to the Web in WPF | Main | The Dispose Pattern »

January 22, 2008

Using Process.Start to link to the Internet

The easiest way to navigate the user's default Internet browser to a specified URL is to call System.Diagnostics.Process.Start(string).

Process.Start("http://www.logos.com/");

The MSDN documentation seems inaccurate on a few points. It suggests that this method should only be called from an STA thread, which isn't true – it will create a new STA thread and run from there if necessary. Worse, a comment in the example suggests that this method doesn't work with a URL to launch an Internet browser – obviously, it works just fine.

We have found that, in some circumstances, with some browsers, Process.Start will raise an exception, even though it might actually succeed, so we wrap the call in a try/catch block and hope for the best.

try

{

    Process.Start("http://www.logos.com/");

}

catch (Exception)

{

}

Interestingly, when used to launch the default Internet browser in this way, Process.Start "waits" until the Internet browser is displayed before returning. I must use quotation marks here, because when Process.Start is called from a UI thread (e.g., the main STA thread of a WPF application), it pumps messages while it waits, which means that the user can still interact with the application, and events like mouse clicks and keystrokes will be processed by the application while it is "waiting". So, make sure that you don't do any work after calling Process.Start that could fail due to user activity in the meantime. Process.Start is probably being called from an event handler, so the best thing to do after calling Process.Start is nothing else.

One thing that bothers me about most applications that link to the Internet is that the Internet browser can take a while to appear. You click on a link, but nothing happens, and you wonder if you actually clicked the link, so you click again, and ultimately end up opening the Web site twice. In a WPF application, a nice way to deal with this problem is to set Mouse.OverrideCursor to Cursors.AppStarting while Process.Start is running. (Fortunately, since Process.Start pumps messages, it doesn't "hang" the application while you wait.)

try

{

    Mouse.OverrideCursor = Cursors.AppStarting;

    Process.Start("http://www.logos.com/");

}

catch (Exception)

{

}

finally

{

    Mouse.OverrideCursor = null;

}

Wrap that up in a utility method and you're good to go.

Posted by Ed Ball at January 22, 2008 9:09 AM

Trackback Pings

TrackBack URL for this entry:
http://blog.logos.com/mt-cgi/mt-tb.cgi/176

Comments


I love your blog and software. I came across it while doing a search for Process.Start and really enjoy the insight that you give. I do have a question however, would Process.Start("http://www.logos.com/"); work in an asp.net 3.5 application? The reason that I ask is when I am running it on my local server it works just fine, however when I upload it to my web server it just does a post refresh of the screen. I have tried Firefox, IE 7, Safari, and Opera all with the same results.

If you can give me any guidance I would greatly appreciate it.

Posted by: Alex Birden at January 25, 2008 10:55 AM

Calling Process.Start from an ASP.NET application will run the Internet browser on the server, not the client, so it isn't very useful in that secnario.

Posted by: Ed Ball at January 28, 2008 8:48 AM

Post a comment




(you may use HTML tags for style)