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 on January 22, 2008