Windows 应用程序中的等待窗口

发布于 2024-07-07 10:09:22 字数 282 浏览 6 评论 0原文

我基本上需要向用户显示一个等待窗口。 为此,我在应用程序中放置了两个单独的窗口窗体。 第一个表单是带有按钮的主表单。 第二个是空的,只有标签文本。 单击 Form1 中的按钮后,我执行以下操作

Form2 f = new Form2();
f.Show();
Thread.Sleep(2000);
f.Close();

我的想法是向用户显示等待窗口 2 秒。 但是当我这样做时,Form 2 没有完全加载,因为其中的标签是空白的。 请让我知道您对此的意见。

I basically need to show a wait window to the user. For this i have put two seperate window forms in the application. the first form is the main form with a button. The second one is a empty one with just a label text. On click of the button in Form1 i do the below

Form2 f = new Form2();
f.Show();
Thread.Sleep(2000);
f.Close();

My idea here is to show the wait window to the user for 2 second. But when i do this the Form 2 is not completely loaded because of which the label in it is blank. Please let me know your inputs on this.

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

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

发布评论

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

评论(6

扭转时空 2024-07-14 10:09:22

这是我使用的等待箱类。 下面是如何使用它:

using WaitingBox;
ShowWaitingBox waiting = new ShowWaitingBox("Title Text","Some Text so the user knows whats going on..");
waiting.Start();
//do something that takes a while
waiting.Stop();

这是 WaitingBox 的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;



    namespace WaitingBox
    {
            public class ShowWaitingBox
            {
                    private class WaitingForm:Form
                    {
                            public WaitingForm()
                            {
                                    this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
                                    this.label1 = new System.Windows.Forms.Label();
                                    this.progressBar1 = new System.Windows.Forms.ProgressBar();
                                    this.tableLayoutPanel1.SuspendLayout();
                                    this.SuspendLayout();
                                    // 
                                    // tableLayoutPanel1
                                    // 
                                    this.tableLayoutPanel1.ColumnCount = 1;
                                    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                                    this.tableLayoutPanel1.Controls.Add(this.progressBar1, 0, 0);
                                    this.tableLayoutPanel1.Controls.Add(this.label1, 0, 2);
                                    this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
                                    this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
                                    this.tableLayoutPanel1.Name = "tableLayoutPanel1";
                                    this.tableLayoutPanel1.RowCount = 3;
                                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
                                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29F));
                                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
                                    this.tableLayoutPanel1.Size = new System.Drawing.Size(492, 155);
                                    this.tableLayoutPanel1.TabIndex = 0;
                                    // 
                                    // label1
                                    // 
                                    this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
                                    this.label1.AutoSize = true;
                                    this.label1.Location = new System.Drawing.Point(209, 92);
                                    this.label1.Name = "label1";
                                    this.label1.Size = new System.Drawing.Size(73, 13);
                                    this.label1.TabIndex = 3;
                                    this.label1.Text = "Please Wait...";
                                    // 
                                    // progressBar1
                                    // 
                                    this.progressBar1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
                                    this.progressBar1.Location = new System.Drawing.Point(22, 37);
                                    this.progressBar1.Name = "progressBar1";
                                    this.progressBar1.Size = new System.Drawing.Size(447, 23);
                                    this.progressBar1.TabIndex = 2;
                                    // 
                                    // WaitingForm
                                    // 
                                    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                                    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                                    this.ClientSize = new System.Drawing.Size(492, 155);
                                    this.Controls.Add(this.tableLayoutPanel1);
                                    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                                    this.Name = "WaitingForm";
                                    this.Text = "Working in the background";
                                    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.WaitingForm_FormClosing);
                                    this.Load += new System.EventHandler(this.WaitingForm_Load);
                                    this.tableLayoutPanel1.ResumeLayout(false);
                                    this.tableLayoutPanel1.PerformLayout();
                                    this.ResumeLayout(false);
                            }

                            private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
                            private System.Windows.Forms.ProgressBar progressBar1;
                            private System.Windows.Forms.Label label1;

                            private void WaitingForm_Load(object sender, EventArgs e)
                            {
                                    progressBar1.Style = ProgressBarStyle.Marquee;
                                    this.BringToFront();
                                    this.CenterToScreen();
                            }

                            private void WaitingForm_FormClosing(object sender, FormClosingEventArgs e)
                            {
                            }

                            internal void SetLabel(string p)
                            {
                                    label1.Text = p;
                            }
                    }
                    private WaitingForm wf = new WaitingForm();
                    private string title, text;
                    private Thread waiting;
                    public bool IsAlive
                    {
                            get
                            {
                                    return waiting.IsAlive;
                            }
                            set { }
                    }
                    public ShowWaitingBox(string Title, string Text)
                    {
                            this.title = string.IsNullOrEmpty(Title) ? "Working in the Background..": Title;
                            this.text = string.IsNullOrEmpty(Text) ? "Please wait..." : Text;
                            waiting = new Thread(new ThreadStart(Show));

                    }

                    public ShowWaitingBox()
                    {
                            waiting = new Thread(new ThreadStart(Show));
                    }

                    private void Show()
                    {
                            wf.Show();
                            wf.Text = this.title;
                            wf.SetLabel(this.text);
                            while (true) {

                                    wf.Refresh();
                                    System.Threading.Thread.Sleep(50);
                            }
                    }
                    public void Start()
                    {
                            waiting.Start();
                    }
                    public void Stop()
                    {
                            waiting.Abort();
                    }
            }
    }

Here is a Waiting Box class I use. Here is how you use it:

using WaitingBox;
ShowWaitingBox waiting = new ShowWaitingBox("Title Text","Some Text so the user knows whats going on..");
waiting.Start();
//do something that takes a while
waiting.Stop();

Here is the code for WaitingBox:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;



    namespace WaitingBox
    {
            public class ShowWaitingBox
            {
                    private class WaitingForm:Form
                    {
                            public WaitingForm()
                            {
                                    this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
                                    this.label1 = new System.Windows.Forms.Label();
                                    this.progressBar1 = new System.Windows.Forms.ProgressBar();
                                    this.tableLayoutPanel1.SuspendLayout();
                                    this.SuspendLayout();
                                    // 
                                    // tableLayoutPanel1
                                    // 
                                    this.tableLayoutPanel1.ColumnCount = 1;
                                    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                                    this.tableLayoutPanel1.Controls.Add(this.progressBar1, 0, 0);
                                    this.tableLayoutPanel1.Controls.Add(this.label1, 0, 2);
                                    this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
                                    this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
                                    this.tableLayoutPanel1.Name = "tableLayoutPanel1";
                                    this.tableLayoutPanel1.RowCount = 3;
                                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
                                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29F));
                                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
                                    this.tableLayoutPanel1.Size = new System.Drawing.Size(492, 155);
                                    this.tableLayoutPanel1.TabIndex = 0;
                                    // 
                                    // label1
                                    // 
                                    this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
                                    this.label1.AutoSize = true;
                                    this.label1.Location = new System.Drawing.Point(209, 92);
                                    this.label1.Name = "label1";
                                    this.label1.Size = new System.Drawing.Size(73, 13);
                                    this.label1.TabIndex = 3;
                                    this.label1.Text = "Please Wait...";
                                    // 
                                    // progressBar1
                                    // 
                                    this.progressBar1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
                                    this.progressBar1.Location = new System.Drawing.Point(22, 37);
                                    this.progressBar1.Name = "progressBar1";
                                    this.progressBar1.Size = new System.Drawing.Size(447, 23);
                                    this.progressBar1.TabIndex = 2;
                                    // 
                                    // WaitingForm
                                    // 
                                    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                                    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                                    this.ClientSize = new System.Drawing.Size(492, 155);
                                    this.Controls.Add(this.tableLayoutPanel1);
                                    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                                    this.Name = "WaitingForm";
                                    this.Text = "Working in the background";
                                    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.WaitingForm_FormClosing);
                                    this.Load += new System.EventHandler(this.WaitingForm_Load);
                                    this.tableLayoutPanel1.ResumeLayout(false);
                                    this.tableLayoutPanel1.PerformLayout();
                                    this.ResumeLayout(false);
                            }

                            private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
                            private System.Windows.Forms.ProgressBar progressBar1;
                            private System.Windows.Forms.Label label1;

                            private void WaitingForm_Load(object sender, EventArgs e)
                            {
                                    progressBar1.Style = ProgressBarStyle.Marquee;
                                    this.BringToFront();
                                    this.CenterToScreen();
                            }

                            private void WaitingForm_FormClosing(object sender, FormClosingEventArgs e)
                            {
                            }

                            internal void SetLabel(string p)
                            {
                                    label1.Text = p;
                            }
                    }
                    private WaitingForm wf = new WaitingForm();
                    private string title, text;
                    private Thread waiting;
                    public bool IsAlive
                    {
                            get
                            {
                                    return waiting.IsAlive;
                            }
                            set { }
                    }
                    public ShowWaitingBox(string Title, string Text)
                    {
                            this.title = string.IsNullOrEmpty(Title) ? "Working in the Background..": Title;
                            this.text = string.IsNullOrEmpty(Text) ? "Please wait..." : Text;
                            waiting = new Thread(new ThreadStart(Show));

                    }

                    public ShowWaitingBox()
                    {
                            waiting = new Thread(new ThreadStart(Show));
                    }

                    private void Show()
                    {
                            wf.Show();
                            wf.Text = this.title;
                            wf.SetLabel(this.text);
                            while (true) {

                                    wf.Refresh();
                                    System.Threading.Thread.Sleep(50);
                            }
                    }
                    public void Start()
                    {
                            waiting.Start();
                    }
                    public void Stop()
                    {
                            waiting.Abort();
                    }
            }
    }
雨夜星沙 2024-07-14 10:09:22

那是因为您可能在同一个线程(UI 线程)中执行一些冗长的操作。 您应该在新线程中执行代码(请参阅 Thread 类),或者至少从冗长的操作中定期调用 Application.DoEvents 来更新 UI。

That's because you probably do some lengthy operation in the same thread (UI thread). You should execute your code in a new thread (see Thread class) or at least call Application.DoEvents periodically from inside your lengthy operation to update the UI.

黑寡妇 2024-07-14 10:09:22

当您使用 Thread.Sleep 时,您将禁用 Windows 消息循环并阻止窗口自行绘制。

您可以强制重新绘制:

f.Refresh();

或者更好的是使用带有回调的计时器。

Timer t = new Timer();
t.Interval = 2000;
t.Tick += delegate { Close(); t.Stop();};
t.Start();

为了防止用户在原始窗口中单击,您可以将新表单作为对话框打开:

f.ShowDialog();

When yo use Thread.Sleep you will disable the windows message loop and prevent the window from painting itself.

You could force a repaint:

f.Refresh();

Or better yet use a timer with a callback.

Timer t = new Timer();
t.Interval = 2000;
t.Tick += delegate { Close(); t.Stop();};
t.Start();

To prevent users from clicking in the original window you can open the new form as a dialog:

f.ShowDialog();
◇流星雨 2024-07-14 10:09:22

你基本上阻塞了 UI 线程。

我建议您让 Form2 构造函数(或者可能是 Load 事件处理程序)启动一个计时器,该计时器将在两秒后触发。 当计时器触发时,关闭窗体。 在那两秒钟内,UI 线程将空闲,因此所有内容都将正确显示,并且用户将能够移动窗口等。

You're basically blocking the UI thread.

I suggest that instead, you make your Form2 constructor (or possibly Load event handler) start a timer which will fire two seconds later. When the timer fires, close the form. During those two seconds, the UI thread will be free, so everything will display properly and the user will be able to move the window etc.

尽揽少女心 2024-07-14 10:09:22

我认为你应该使用

f.ShowDialog(this);

Then control is returned when f is close 。

通过使用睡眠,您将阻止 UI 线程更新 2 秒。 (线程处于休眠状态)。

I think you should just use

f.ShowDialog(this);

Then control is returned when f is closed.

By using the sleep, you are blocking the UI thread from updating for 2 seconds. (The thread is asleep).

勿忘初心 2024-07-14 10:09:22

您可以(对于 UI 线程始终应该)使用 Thread.Current.Join(2000 ) 而不是 Thread.Sleep(2000)。

You can (an always should for UI threads) use Thread.Current.Join(2000) instead of Thread.Sleep(2000).

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