Mono 异步加载数据更新主线程

主要是用 async await 调用 RunOnUiThread来更新。

调用函数:

 

//异步加载数据开始

doInBackground ();

//异步加载数据开始end

protected async void doInBackground ()

{

       var result = await Task.Factory.StartNew((Func<string>)onSuccessfulLogin);



                if (result=="") {



                    await Task.Factory.StartNew (() => {

                        RunOnUiThread (delegate() {

                            //跳转到主页面

                            Intent intent = new Intent();

                            intent.SetClass(this, typeof(Main));

                            StartActivity(intent);

                            Finish ();

                        });

                    });

                }

}

    protected string  onSuccessfulLogin ()

        {





            try {

                Thread.Sleep (2000);

                Hashtable pars = new Hashtable();

                pars.Add("UserName",mAccount);

                pars.Add("Password", mPassword);

                UserInfoDao userinfodao = new UserInfoDao ();

                userinfomodel = userinfodao.getWebservice (pars);



                //判断登录



                if (userinfomodel != null) {

                    if (userinfomodel.UserID > 0) {



                        //保持登录的状态 bgein

                        ISharedPreferences shared = GetSharedPreferences("UserInfo", FileCreationMode.Private);

                        string value = shared.GetString("UserInfo", "");

                        value =userinfomodel.UserID.ToString().Trim()+"|"+userinfomodel.UserName+"|"+userinfomodel.Token ;

                        ISharedPreferencesEditor editor = shared.Edit();

                        editor.PutString("UserInfo",value);

                        editor.Commit();

                        //保持登录的状态 end



                    }

                }

                else

                {

                    return "用户名或密码错误!";

                }

            } catch (Exception e) {

                return e.ToString();

            }



            return "";



        }

 

 

 

你可能感兴趣的:(异步加载)