partial class LoadingForm { /// <summary> /// 堆代码 duidaima.com /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoadingForm)); pictureBox1 = new PictureBox(); messageLab = new Label(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); SuspendLayout(); // // pictureBox1 // pictureBox1.BackColor = Color.White; pictureBox1.Dock = DockStyle.Top; pictureBox1.Image = (Image)resources.GetObject("pictureBox1.Image"); pictureBox1.Location = new Point(0, 0); pictureBox1.Name = "pictureBox1"; pictureBox1.Size = new Size(647, 141); pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.TabIndex = 0; pictureBox1.TabStop = false; // // messageLab // messageLab.BackColor = Color.Azure; messageLab.Dock = DockStyle.Fill; messageLab.Font = new Font("Microsoft YaHei UI", 12F, FontStyle.Bold, GraphicsUnit.Point); messageLab.Location = new Point(0, 141); messageLab.Name = "messageLab"; messageLab.Padding = new Padding(30, 40, 30, 5); messageLab.Size = new Size(647, 153); messageLab.TabIndex = 1; messageLab.Text = "正在加载中,请等待……"; // // LoadingForm // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(647, 294); Controls.Add(messageLab); Controls.Add(pictureBox1); Name = "LoadingForm"; ShowIcon = false; ShowInTaskbar = false; StartPosition = FormStartPosition.CenterScreen; Text = "Loading"; TopMost = true; ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit(); ResumeLayout(false); } #endregion private PictureBox pictureBox1; private Label messageLab; }为了外部的实时更新提示信息,定义一个Message属性,来更新内容的提示信息控件,这里用的是this.Invoke,因为UI线程有可能与执行线程不相同。同理,当关闭窗体时,也需要处理一下。
public partial class LoadingForm : Form { public LoadingForm() { InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None; } public string Message { set { this.Invoke(() => { messageLab.Text = value; }); } } public void CloseAll() { this.Invoke(() => { this.Close(); this.Dispose(); }); } }第二步:定义一个等待窗体的管理类,主要有三个成员:1、Show(),是在一个线程中;2、Message属性,是设置窗体的属性,这里需要判断窗体是否存在,不存在就等待,其实可以定义重试次数,避免错误调用顺序,设置属性也是在一个线程中执行;3、Close()也是在线程中,也需要判断窗体是否存在。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WaitForm { public static class LoadingManage { static LoadingForm _loading; public static void Show() { Task.Run(() => { _loading = new LoadingForm(); _loading.ShowDialog(); }); } public static void Close() { Task.Run(() => { while (_loading == null || _loading.IsDisposed) { Thread.Sleep(10); } _loading.CloseAll(); }); } public static string Message { set { while (_loading == null||_loading.IsDisposed) { Thread.Sleep(10); } Task.Run(() => { _loading.Message = value; }); } } } }封装后,调用就简单了,如下,首先Show(),然后执行密集服务,在执行过程中,可以更新Message属性,最后执行完就可以Close()了。
LoadingManage.Show(); for (var i = 0; i < 3; i++) { LoadingManage.Message = $"正在执行第{i}条数据……"; Thread.Sleep(1000); } LoadingManage.Close();
下面是准备果图: