How to continue executing code after calling ShowDialog()

后端 未结 7 785
走了就别回头了
走了就别回头了 2020-12-17 10:58

the Form.ShowDialog() method causes the code to be halted until the newly called form is closed. I need the code to continue running after the ShowDialog() method is called.

相关标签:
7条回答
  • 2020-12-17 11:36

    This is my way, so ugly but i have no better idea.

    private void AppUiMain_Shown(object sender, EventArgs e)
    {
        var loading = new AppUiLoading();
        loading.Shown += (o, args) =>
        {
            bool isLoading = true;
            loading.Top = (int)(loading.Top * 1.16);
    
            Application.DoEvents();//refresh ui
    
            EventHandler ehr = null;
            EventHandler ehe = null;
            ehr = (ss, ee) =>
            {
                App.Instance.Ready -= ehr;
                App.Instance.Error -= ehe;
                isLoading = false;
            };
            ehe = (ss, ee) =>
            {
                loading.Text = "Error";
                loading.ShowAbortButton("Error occur");
            };
            App.Instance.Error += ehe;
            App.Instance.Ready += ehr;
            InitApp();
    
            //HACK: find a better way to `refresh' main form
            Application.DoEvents();
            this.Height++;
            this.Height--;
    
            //HACK: find a better way to keep message looping on ShowDialog
            while (isLoading)
                Application.DoEvents();
    
            loading.Close();
        };
        loading.ShowDialog(this);
    }
    
    0 讨论(0)
提交回复
热议问题