隐形父母的孩子形式永远不会被显示?

发布于 2024-11-02 13:46:52 字数 2901 浏览 3 评论 0原文

我有一个主窗体,它是不可见的,并且在某些时候会创建一个子窗体。该子窗体在 Designer.cs 中如下所示:

        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        //this.listBox1.Location = new System.Drawing.Point(0, 0);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(255, 147);
        this.listBox1.TabIndex = 0;
        // 
        // Form2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.ClientSize = new System.Drawing.Size(502, 384);
        this.ControlBox = false;
        this.Controls.Add(this.listBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form2";
        this.Text = "Form2";

在主窗体中,我创建 Form2 如下:

    Form2 a = new Form2(new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2)); //This is the location
                        Thread thtt = new Thread((ThreadStart)delegate()
                            {
                                a.CreateControl();
                                a.Show();
                                TimeSpan slept = TimeSpan.Zero;
                                while (!a.Created && slept < TimeSpan.FromMilliseconds(2000))
                                {
                                    Thread.Sleep(99);
                                    slept += TimeSpan.FromMilliseconds(99);
                                }
                                if (!a.Created)
                                {
                                    MessageBox.Show("after 2 sec no creation?");
                                    System.Diagnostics.Debugger.Break();
                                }
                                else
                                {
                                    //a.Show();
                                    a.Invoke((MethodInvoker)delegate()
                                    {

                                        a.TopMost = true;
                                        a.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2);
                                        Cursor.Position = new Point(a.Location.X + 10, a.Location.Y + 10);
                                        a.AddAndAct(minimized);

                                    });
                                }
                                aa = a;
                            });
                        thtt.IsBackground = true;
                        thtt.Start();

我遇到的问题是,Form2 实际上在闪烁,但随后神奇地消失了。任何建议都适用。

谢谢你的建议,亚历克斯

I have a main form, which is invisible and at some point creates a child form. That child form looks like this in designer.cs:

        this.listBox1 = new System.Windows.Forms.ListBox();
        this.SuspendLayout();
        // 
        // listBox1
        // 
        this.listBox1.FormattingEnabled = true;
        //this.listBox1.Location = new System.Drawing.Point(0, 0);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(255, 147);
        this.listBox1.TabIndex = 0;
        // 
        // Form2
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.ClientSize = new System.Drawing.Size(502, 384);
        this.ControlBox = false;
        this.Controls.Add(this.listBox1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form2";
        this.Text = "Form2";

and in the main form I'm creating the Form2 as follows:

    Form2 a = new Form2(new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2)); //This is the location
                        Thread thtt = new Thread((ThreadStart)delegate()
                            {
                                a.CreateControl();
                                a.Show();
                                TimeSpan slept = TimeSpan.Zero;
                                while (!a.Created && slept < TimeSpan.FromMilliseconds(2000))
                                {
                                    Thread.Sleep(99);
                                    slept += TimeSpan.FromMilliseconds(99);
                                }
                                if (!a.Created)
                                {
                                    MessageBox.Show("after 2 sec no creation?");
                                    System.Diagnostics.Debugger.Break();
                                }
                                else
                                {
                                    //a.Show();
                                    a.Invoke((MethodInvoker)delegate()
                                    {

                                        a.TopMost = true;
                                        a.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height / 2);
                                        Cursor.Position = new Point(a.Location.X + 10, a.Location.Y + 10);
                                        a.AddAndAct(minimized);

                                    });
                                }
                                aa = a;
                            });
                        thtt.IsBackground = true;
                        thtt.Start();

The problem I'm encountering is, that the Form2 is actually flashing, but then disappears magically. Any suggestions appriciated.

Thanks in advice, Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长安忆 2024-11-09 13:46:52

亚历克斯 - 我认为这是你的问题:

如何在自己的线程上创建表单并在整个应用程序生命周期中保持打开状态

接受的答案非常正确,因为我也在我的应用程序中使用它,并且忘记了它。 =)

“您需要在新创建的线程上启动消息循环。您可以通过调用 Application.Run(form) 来做到这一点。”

Alex - I think this is your problem:

How to create a form on its own thread and keep it open throughout application lifetime

The accepted answer is very true, for I use it in my applications as well, and had forgotten about it. =)

"You will need to start a message loop on the newly created thread. You can do that by calling Application.Run(form)."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文