I’ve got a F# application, where I need to open an URL in the user’s default browser. This is all well and good, and is handled just fine by the following method:
member this.openUrl (url:Uri) =
ignore (System.Diagnostics.Process.Start(url.ToString()))
Upon opening the URL, however, the browser takes focus, taking the user away from my program. This is sub-optimal, as it’s likely that the user would like to open several pages in quick succession. In that case, the browser getting focus, making the user have to manually switch back to my application. This disturbs the intended workflow of my application.
Is there a way to prevent the default browser from becoming active when asking it to open an URL?
You can assume that I’ll add an option for disabling this behavior, if you’re worried about the user experience.
AppActive does what you need.
Dont forget to add a reference to Microsoft.VisualBasic in F#
AppActivate http://msdn.microsoft.com/en-us/library/x9784w8e.aspx
Many user are facing problems with AppActivate when running Console programs from Command Line. The answer to this follows (from AppActivate documentantion):
You can use AppActivate only with processes that own windows. Most console applications do not own windows, which means that they do not appear in the list of processes that AppActivate searches. When running from a console application, the system creates a separate process to run the application and returns the output to the console process. Consequently, when you request the current process ID, you get the process ID of this separate process, rather than the console application’s process ID.