简体   繁体   English

实时连接 - 异步和等待阻止UI

[英]Live Connect - async and await blocking UI

Now I think I understand why this happening so I guess my question is how can I modify the code to act how I want. 现在我想我明白为什么会发生这种情况所以我想我的问题是如何修改代码以实现我想要的方式。

First the code. 首先是代码。

private void btnLiveSignin_Click(object sender, RoutedEventArgs e)
{
    var LoggedIn = Login();

    busy.Visibility = System.Windows.Visibility.Visible;  // Display the please wait message

    if (LoggedIn.Result)
    {
        // Do something 
    }
}

public async Task<bool> Login()
{
    try
    {
        var e = await lac.InitializeAsync(Scopes);

        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            client = new LiveConnectClient(e.Session);

            // do some stuff now that we are connected

            return true;
        }
        else
        {
            // not connected so do something

            return false;
        }
    }
    catch (Exception ex)
    {
        return false;
    }
}

Basically when the user clicks a button it will log them in to their windows live account and while that is happening a message appears asking the user to please wait. 基本上当用户点击一个按钮时,它会将它们登录到他们的Windows真实账户,当发生这种情况时会出现一条消息,要求用户请等待。

As you can guess the UI locks so the message never appears. 您可以猜到UI锁定,因此消息永远不会出现。 How can I change it so the UI doesn't lock? 如何更改它以便UI不锁定?

I done this with windows phone 7.5 before using events as the sdk didn't use async, and that worked perfectly fine. 我在使用事件之前使用windows phone 7.5完成了这个,因为sdk没有使用异步,并且工作得很好。 Would this require a mixture of asyncing and events? 这需要混合使用异步和事件吗?

What you're missing from your handler is async and await . 您在处理程序中缺少的是asyncawait

Because you marked your Login method as async and made it return a task, you've made it awaitable , which makes it possible to re-write your event-handler as: 因为您将Login方法标记为异步并使其返回任务,所以您已将其设置为等待 ,这使得可以将事件处理程序重新编写为:

private async void btnLiveSignin_Click(object sender, RoutedEventArgs e)
{
    var loginTask = this.Login();

    busy.Visibility = System.Windows.Visibility.Visible; 

    await loginTask;

    if(loginTask.Result)
    {
       // Do something 
    }
}

Marking the event-handler as async indicates to the caller that it will complete asynchronously. 将事件处理程序标记为async会向调用者指示它将异步完成。 What await does is "signal" that you want to wait for the task to complete before continuing. await做的是“信号”,您希望在继续之前等待任务完成。 Together this means that the event-handler can return an "incomplete" result back to the dispatcher (the UI thread) which will run in the background without disrupting the UI. 这意味着事件处理程序可以将“不完整”结果返回给调度程序(UI线程),调度程序将在后台运行而不会中断UI。

As a rule, once you start using async and await in your code, you should try using them "all the way down". 通常,一旦您开始在代码中使用asyncawait ,您应该尝试“一直使用”它们。

async and await are a bit devious in how the compiler turns them into "real" code, so don't be surprised if you need to read more around the topic to understand them fully. asyncawait在编译器如何将它们变成“真实”代码时有点狡猾,所以如果你需要阅读更多关于主题的内容以便完全理解它们,不要感到惊讶。

You need to await the task rather than doing a blocking wait (which is want Result does). 您需要await任务而不是阻塞等待(这需要Result )。

private async void btnLiveSignin_Click(object sender, RoutedEventArgs e)
{
    busy.Visibility = System.Windows.Visibility.Visible;  // Display the please wait message

    if(await Login())
    {
       // Do something 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM