locked
LaunchUriAsync always returns FALSE for a simple HTTP URI?

    Question

  • I am trying to launch the browser in a Windows Store App using LaunchUriAsync().  It always returns FALSE after the await statement.  I tried setting it to display an launch app picker, in case it was a browser issue.  But it never shows the browser and still returns FALSE.  What can I do to fix this?

            async static private Task DoLaunchBrowserWithUri(Uri theUri)
            {
                LauncherOptions launchOpts = new LauncherOptions()
                    {
                        DisplayApplicationPicker = true
                    };
    
                bool success = await Windows.System.Launcher.LaunchUriAsync(theUri, launchOpts);
    
                if (success)
                    Debug.WriteLine("URI launch succeeded.");
                else
                    Debug.WriteLine("URI launch failed.");
            }
    
            public static void LaunchBrowserWithUri(Uri theUri)
            {
                Task.Run(() => DoLaunchBrowserWithUri(theUri));
            }



    -- roschler

    Tuesday, February 18, 2014 4:31 PM

Answers

  • You need to call LaunchUriAsync from your UI thread. It should work fine if you don't throw it in a separate Task.Run.

    Is there a reason you want to use that extra level of redirection? It doesn't appear to add anything over calling DoLaunchBrowserWithUri directly.

    As a stylistic note, since DoLaunchBrowserWithUri returns a Task it should be called DoLaunchBrowserWithUriAsync. If you don't plan on awaiting it anywhere it doesn't need to return the Task.

    --Rob

    • Marked as answer by roschler Wednesday, February 19, 2014 12:00 AM
    Tuesday, February 18, 2014 5:08 PM
    Owner

All replies

  • You need to call LaunchUriAsync from your UI thread. It should work fine if you don't throw it in a separate Task.Run.

    Is there a reason you want to use that extra level of redirection? It doesn't appear to add anything over calling DoLaunchBrowserWithUri directly.

    As a stylistic note, since DoLaunchBrowserWithUri returns a Task it should be called DoLaunchBrowserWithUriAsync. If you don't plan on awaiting it anywhere it doesn't need to return the Task.

    --Rob

    • Marked as answer by roschler Wednesday, February 19, 2014 12:00 AM
    Tuesday, February 18, 2014 5:08 PM
    Owner
  • Hello Rob,

    I find myself in a ongoing dance to avoid getting the "The async method lacks await operators" compiler warning when I want to execute a "fire-and-forget" async task.  Lately I've been wrapping such async marked methods in Task.Run() statements to let WinRT know that I don't need a continuation context.  Frequently I execute these "fire-and-forget" tasks in methods where I can't wait on the return so using await isn't a viable alternative.  If you have suggestions on other ways to do this that are safe for the UI thread, please let me know.  For now I'll just go back to executing LaunchUriAsync() without Task.Run() and live with the compiler warning.

    Thanks for the tip on the naming convention.


    -- roschler

    Wednesday, February 19, 2014 12:05 AM
  • A better way would be to await the task rather than wrapping it in Task.Run. You could also just ignore the warning if you know it's not relevant.

    --Rob

    Wednesday, February 19, 2014 1:19 AM
    Owner