异步伺机没有使用Visual Studio和Xamarin等待的过程 [英] Async await not wait the process using Visual Studio and Xamarin

查看:235
本文介绍了异步伺机没有使用Visual Studio和Xamarin等待的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我不异步等待的过程,这里是我的code:

i don't know why my async not waiting the process, here is my code :

userKu = new UserData();
btnLogin = FindViewById<Button>(Resource.Id.buttonLogin);

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
    if (isOnline() == true)
    {
        AppPreferences ap = new AppPreferences(mContext);
        string usernameCekLogin = ap.getNamaUser(); 

        //TOAST #1
        Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
        isLogin(usernameCekLogin, userKu);

        //TOAST #2
        Toast.MakeText(this, userKu.status, ToastLength.Long).Show();

        btnLogin.Click +=  delegate{}
    }
}

private async Task<string> FetchUserAsync(string url)
{
    // Create an HTTP web request using the URL:
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
    request.ContentType = "application/json";
    request.Method = "GET";

    // Send the request to the server and wait for the response:
    using (WebResponse response = await request.GetResponseAsync())
    {
        // Get a stream representation of the HTTP web response:
        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            string strContent = sr.ReadToEnd();
            return strContent;
        }
    }
}

private async void isLogin(string username, UserData data1)
{
    string url = "http://localhost.com/api.php/isLogin/login_mbr/"+username;

    data1 = JsonConvert.DeserializeObject<UserData>(await FetchUserAsync(url));

    //TOAST 3
    Toast.MakeText(this, "check user status = "+data1.status, ToastLength.Long).Show();
}

当我运行它,我没有得到#TOAST 1和2 #TOAST的价值,但在#TOAST 3我得到的值0或1。

When I run it, I don't get the value on #TOAST 1 and #TOAST 2, but on #TOAST 3 I get the value "0" or "1".

如果我变化保护覆盖异步无效的onCreate(捆绑包),我不使用这样的功能:

If I change protected override async void OnCreate(Bundle bundle) and I don't use function like this :

userKu = new UserData();
btnLogin = FindViewById<Button>(Resource.Id.buttonLogin);

protected override async void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);
    if (isOnline() == true)
    {
        string url = "http://localhost.com/api.php/isLogin/login_mbr/";
        AppPreferences ap = new AppPreferences(mContext);

        string usernameCekLogin = ap.getNamaUser(); 
        url += usernameCekLogin;
        //TOAST 1
        Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
        userKu = JsonConvert.DeserializeObject<UserData>(await FetchUserAsync(url));
        //TOAST 2
        Toast.MakeText(this, userKu.status, ToastLength.Long).Show();
        /*
            btnLogin.Click +=  delegate{}*/
    }
}

private async Task<string> FetchUserAsync(string url)
{
    // Create an HTTP web request using the URL:
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
    request.ContentType = "application/json";
    request.Method = "GET";

    // Send the request to the server and wait for the response:
    using (WebResponse response = await request.GetResponseAsync())
    {
        // Get a stream representation of the HTTP web response:
        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            string strContent = sr.ReadToEnd();
            return strContent;
        }
    }
}

在#Toast 1我没有得到的价值和#Toast 2我得到的价值。但我得到的错误,当我使我的按钮,故称空引用。任何解决方案为我的问题呢?我使用Visual Studio 2015年和Xamarin开发Android应用。

On #Toast 1 I don't get value and on #Toast 2 I get the value. But I get error when I enable my button, it said null references. Any Solution for my problems? I'm using Visual Studio 2015 and Xamarin for developing android app.

在此先感谢

推荐答案

等待关键字返回控制已执行异步方法的线程。您可能需要使用ContinueWith关键字

await keyword returns the control to the thread which has executed async method. You may want to use ContinueWith keyword

var task = FetchUserAsync(url).ContinueWith((t) => 
{
    // this will be done after task completes, so after you get response from web   service
    Toast.MakeText(this, t.Result.status, ToastLength.Long).Show();
}
// this will run immediately, so it can be done before your task completes

如果你想等到任务完成后,你也可以用

if you want to wait until the task is completed you can also use

task.Wait();

这篇关于异步伺机没有使用Visual Studio和Xamarin等待的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆