在线程中实例化新表单 [英] Instance a new form in a thread

查看:69
本文介绍了在线程中实例化新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我希望在应用程序繁忙时显示类似Splashscreen的内容。我想创建一个开始一个新表单的线程,其中包含一个progess栏和一些用户必须等待的信息。

到目前为止一切都那么好,但表格冻结了,因为它显示了。 (如果我不进行更新,我注释掉了,表单甚至没有正确构建)。



这里有什么问题?



我尝试了什么:



My Problem is, that i want to show something like a Splashscreen while the Application is Busy. I wanted to Create a Thread starting a new form with a progess bar and a little information that the user has to wait.
So far so good, but the form freezes as it show's up. (If I don't do the update, I commented out, the form not even builds properly).

What is the problem here?

What I have tried:

public class WaitFormManager
    {
        private Thread waitFormThread;

        public WaitFormManager()
        {
            waitFormThread = new Thread(new ThreadStart(OpenWaitform));
        }

        public void ShowWaitForm()
        {
            if (!waitFormThread.IsAlive)
            {
                waitFormThread.Start();
            }
        }

        public void HideWaitForm()
        {
            if (waitFormThread.IsAlive)
            {
                waitFormThread.Abort();
            }
        }

        private void OpenWaitform()
        {
            WaitForm wf = new WaitForm();
            wf.Show();
            //wf.Update();
        }
    }





我这样打电话给表格:



I called the Form this way:

public static WaitFormManager wfm = new WaitFormManager();
wfm.ShowWaitForm();
//Here we get some stuff done
System.Threading.Thread.Sleep(2000);
wfm.HideWaitForm();

推荐答案

以showdialog打开UI界面,然后将长时间运行的任务放在Task.Run下或者在任务完成后创建后台线程,然后关闭UI。您无法在新线程下打开UI UI应该由主线程打开。
Open UI screen as showdialog then put your long running task under the Task.Run or create a background thread once your task completed then close your UI. You can't open the UI under a new thread UI should be open by main thread.


这篇关于在线程中实例化新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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