Form.Show() 不显示其子控件

Ref*_*din 3 .net c# multithreading winforms progress-bar

我有一个表单,frmPleaseWait其中包含 aMarqueeProgressBar和 a Label,当 UI 在我们拥有的结构不良的应用程序中加载数据时,我想使用它们。

问题是frmPleaseWait.Show()显示表单但不显示其中的控件。它只是一个白色的矩形。现在frmPleaseWait.ShowDialog()显示子控件,但不允许 UI 加载其数据。

我缺少什么?下面是我正在尝试的代码片段。

        PleaseWait = new frmPleaseWait();
        PleaseWait.Show(this);

        // Set all available HUD values in HUD Object
        HUD.LastName = GetCurrentRowVal("LastName").Trim();
        HUD.FirstName = GetCurrentRowVal("FirstName").Trim();
        HUD.PersonId = Convert.ToInt32(GetCurrentRowVal("PersonID").Trim());
        HUD.SSn = GetCurrentRowVal("SSN").Trim();
        HUD.MiddleName = GetCurrentRowVal("MiddleName").Trim();
        HUD.MasterID = ConnectBLL.BLL.DriInterface.CheckForDriId(HUD.PersonId).ToString();

        // This loads numerous UserControls with data
        shellForm.FormPaint(HUD.PersonId);

        PleaseWait.Close();
Run Code Online (Sandbox Code Playgroud)

编辑

:

根据答案和我的尝试进行跟进。

这就是我所拥有的,但我得到了一个“Cross-Thread Exception如果pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty); 我删除该行,它将运行,但它在我的屏幕的左上角运行并忽略应用程序的位置”。

    public partial class frmPleaseWait : XtraForm
{
    public frmPleaseWait()
    {
        InitializeComponent();
    }

    private static frmPleaseWait pleaseWaitInstance;

    public static void Create(XtraForm parent)
    {
        var t = new System.Threading.Thread(
            () =>
                {
                    pleaseWaitInstance = new frmPleaseWait();
                    pleaseWaitInstance.FormClosed += (s, e) => pleaseWaitInstance = null;
                    pleaseWaitInstance.StartPosition = FormStartPosition.Manual;
                    pleaseWaitInstance.Location = parent.PointToScreen(Point.Empty);
                    Application.Run(pleaseWaitInstance);
                });
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.IsBackground = true;
        t.Start();
    }

    public static void Destroy()
    {
        if (pleaseWaitInstance != null) pleaseWaitInstance.Invoke(new Action(() => pleaseWaitInstance.Close()));
    }
}
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 6

您的表单无法工作的原因与 shellForm 无法工作的原因相同。UI 线程正忙于加载和绘制控件,它无法同时绘制您的 PleaseWait 表单。您需要创建一个单独的线程来泵送消息循环以保持您的 PW 表单处于活动状态。你可以让它像这样工作:

public partial class PleaseWait : Form {
    private static PleaseWait mInstance;
    public static void Create() {
        var t = new System.Threading.Thread(() => {
            mInstance = new PleaseWait();
            mInstance.FormClosed += (s, e) => mInstance = null;
            Application.Run(mInstance);
        });
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.IsBackground = true;
        t.Start();
    }
    public static void Destroy() {
        if (mInstance != null) mInstance.Invoke(new Action(() => mInstance.Close()));
    }

    private PleaseWait() {
        InitializeComponent();
    }
    //etc...
}
Run Code Online (Sandbox Code Playgroud)

使用示例:

        PleaseWait.Create();
        try {
            System.Threading.Thread.Sleep(3000);
        }
        finally {
            PleaseWait.Destroy();
        }
Run Code Online (Sandbox Code Playgroud)