winform / wpf api calls asynchronous correct posture, not the card interface

Api frame with Flurl;

        public async Task<ApiRespone<QueryDocLoginModel>> QueryDocLogin(string ysdm, string pwd)
        {
            try
            {
                Thread.Sleep(5000);
                var responseString = await (ConfigurationManager.AppSettings["ApiHost"] + "/HisApi/Triage/QueryDocLogin")
                    .SetQueryParams(new { ysdm = ysdm, pwd = pwd })
                  .GetJsonAsync<ApiRespone<QueryDocLoginModel>>();
                return responseString;
            }
            catch (Exception e)
            {
                YinLong.Framework.Logs.Log4.Debug("[QueryDocLogin异常]:" + e.ToString());
                return null;
            }
        }

Button events

        private async void ButtonLogin_OnClick(object sender, RoutedEventArgs e)
        {
            Apis apis = new Apis();
            string account = TextBoxAccount.Text;
            string pass = Password.Password;
            ButtonLogin.Content = " Log in " ;
            ButtonLogin.IsEnabled = false;
            ApiRespone<QueryDocLoginModel> model = null;
            await Task.Run(delegate
             {
                 model = apis.QueryDocLogin(account, pass).Result;

             });

            if (model != null)
            {
                Configs.QueryDocLoginModel = model;
                this.Dispatcher.Invoke(new Action(delegate
                {
                    new new the MainWindow () the Show ();. // display the main window; 
                    the Close ();
                }));
            }
            else
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    ButtonLogin.Content = " Login " ;
                    ButtonLogin.IsEnabled = true;
                }));
                MessageBox.Show ( " login failed " );
            }
        }

 

Guess you like

Origin www.cnblogs.com/wangyinlon/p/12012469.html