如何在执行方法Async-Await时显示WatiForm

时间:2017-09-18 06:10:29

标签: c# .net winforms asynchronous async-await

在我的c#winforms应用程序中,在显示表单的同时我在 LoadDataAsync 方法中加载数据,但是在加载数据之前我想开始显示启动画面,这没有发生可以有人指导我,我做错了什么......或任何想法。

 public partial class DepartmentListDetailView : BaseForm
    {
        private DepartmentDataScope _departmentDataScope;
        private AppConfigDataScope _appConfigDataScope;
        private DepartmentSearch _departmentSearch;

        public DepartmentListDetailView() : base()
        {
            InitializeComponent();
            Init();
        }

        private async void Init()
        {
            _departmentDataScope = new DepartmentDataScope();
            _appConfigDataScope = new AppConfigDataScope();
            _departmentSearch = new DepartmentSearch();
            var res = await LoadDataAsync();
        }        

        private async Task<bool> LoadDataAsync()
        {
            Ssm.ShowWaitForm(); // before loading the data, I want to display the spalsh screen           

            var result = await _departmentDataScope.FetchDataAsync();
            BbiSearch.Enabled = true;
            if (Ssm.IsSplashFormVisible)
            {
                this.Invoke((MethodInvoker) Ssm.CloseWaitForm);
            }
            return true;
        }
    }

由于

1 个答案:

答案 0 :(得分:0)

在调用async方法之前显示它,如下面的代码。

       Ssm.ShowWaitForm();
       var res = await LoadDataAsync();
       if (Ssm.IsSplashFormVisible)
        {
            this.Invoke((MethodInvoker) Ssm.CloseWaitForm);
        }