Created
June 15, 2018 01:46
-
-
Save rqx110/b5bc9d8dcf2829d3c0fcf27fa1fa19d1 to your computer and use it in GitHub Desktop.
DevExpress show wait form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class WaitForm : DevExpress.XtraWaitForm.WaitForm | |
{ | |
private static WaitForm waitForm; | |
/// <summary> | |
/// 标识等待窗口是否关闭 | |
/// </summary> | |
private static bool isClose = false; | |
private static bool isCreateThread = false; | |
private static ManualResetEvent waitLock = new ManualResetEvent(false); | |
private static ManualResetEvent loadLock = new ManualResetEvent(false); | |
private static Stack<WaitFormAction> StackAction = new Stack<WaitFormAction>(); | |
private class WaitFormAction | |
{ | |
public enum EnumAction | |
{ | |
Show = 0, | |
Close = 1, | |
Wait = 2 | |
} | |
public WaitFormAction(EnumAction action) | |
{ | |
this.Action = action; | |
} | |
public EnumAction Action { get; set; } | |
public string Description { get; set; } | |
public string Caption { get; set; } | |
public int SleepForShow { get; set; } | |
} | |
public WaitForm(string description,string caption) | |
{ | |
InitializeComponent(); | |
SetDescription(description); | |
SetCaption(caption); | |
} | |
#region 显示Loading窗口 | |
/// <summary> | |
/// 显示Loading窗口 | |
/// </summary> | |
/// <param name="description"></param> | |
/// <param name="caption"></param> | |
/// <param name="sleepForShow"></param> | |
public static void ShowWaitForm(string description = "加载中", string caption = "请稍等", int sleepForShow = 100) | |
{ | |
isClose = false; | |
StackAction.Clear(); | |
StackAction.Push(new WaitFormAction(WaitFormAction.EnumAction.Show) | |
{ | |
Description = description, | |
Caption = caption, | |
SleepForShow = sleepForShow | |
});//添加到栈中 | |
if (!isCreateThread) //如果线程没有创建 | |
{ | |
waitLock.Reset(); | |
isCreateThread = true; | |
Thread thread = new Thread(Running) { IsBackground = true }; | |
thread.Start(); | |
} | |
else | |
{ | |
waitLock.Set(); | |
} | |
} | |
#endregion | |
#region 关闭Loading窗口 | |
/// <summary> | |
/// 关闭Loading窗口 | |
/// </summary> | |
public static void CloseWaitForm() | |
{ | |
isClose = true; | |
StackAction.Clear(); | |
StackAction.Push(new WaitFormAction(WaitFormAction.EnumAction.Close)); | |
waitLock.Set(); | |
} | |
#endregion | |
#region 线程处理Loading窗口 | |
/// <summary> | |
/// 线程处理Loading窗口 | |
/// </summary> | |
private static void Running() | |
{ | |
while (true) | |
{ | |
if (StackAction.Count > 0) | |
{ | |
WaitFormAction waitFormAction; | |
lock (StackAction) | |
{ | |
waitFormAction = StackAction.Pop(); | |
} | |
#region Show | |
if (waitFormAction.Action == WaitFormAction.EnumAction.Show) | |
{ | |
if (waitForm != null) | |
{ | |
if (isClose) | |
continue; | |
try | |
{ | |
ReSetContent(waitFormAction.Description, waitFormAction.Caption, | |
waitFormAction.SleepForShow); | |
} | |
catch | |
{ | |
} | |
} | |
else | |
{ | |
if (waitFormAction.SleepForShow != 0) | |
{ | |
Thread.Sleep(waitFormAction.SleepForShow); | |
} | |
if (isClose) | |
continue; | |
loadLock.Reset(); | |
Thread thread = new Thread(() => | |
{ | |
waitForm = new WaitForm(waitFormAction.Description, | |
waitFormAction.Caption); | |
Application.Run(waitForm); | |
}) { IsBackground = true }; | |
thread.Start(); | |
loadLock.WaitOne(); | |
} | |
} | |
#endregion | |
#region Close | |
else if (waitFormAction.Action == WaitFormAction.EnumAction.Close) | |
{ | |
try | |
{ | |
if (waitForm != null) | |
{ | |
waitForm.Invoke((MethodInvoker)(() => waitForm.Hide())); | |
isClose = true; | |
} | |
} | |
catch (Exception) | |
{ | |
} | |
} | |
#endregion | |
} | |
else | |
{ | |
waitLock.Reset(); | |
waitLock.WaitOne(); | |
} | |
} | |
} | |
#endregion | |
#region 设置Loading窗口的descrition和caption | |
/// <summary> | |
/// 设置Loading窗口的descrition和caption | |
/// </summary> | |
/// <param name="descrition">Loading窗口的descrition</param> | |
/// <param name="caption">Loading窗口的caption</param> | |
/// <param name="sleepForShow">Loading窗口延迟多少秒出现</param> | |
private static void ReSetContent(string descrition, string caption, int sleepForShow) | |
{ | |
if (waitForm != null) | |
{ | |
try | |
{ | |
waitForm.SetContentInvoke(descrition, caption, sleepForShow); | |
} | |
catch (Exception) | |
{ | |
} | |
} | |
} | |
private void SetContentInvoke(string description, string caption, int sleepForShow) | |
{ | |
this.Invoke(new ReSetContentDelete(SetCaption), caption); | |
this.Invoke(new ReSetContentDelete(SetDescription), description); | |
Thread.Sleep(sleepForShow); | |
if (isClose) | |
return; | |
this.Invoke((MethodInvoker)(() => | |
{ | |
this.Show(); | |
this.Activate(); | |
})); | |
} | |
#endregion | |
private delegate void ReSetContentDelete(string content); | |
public override sealed void SetCaption(string caption) | |
{ | |
this.progressPanel1.Caption = caption; | |
} | |
public override sealed void SetDescription(string description) | |
{ | |
this.progressPanel1.Description = description; | |
} | |
private void WaitForm_Shown(object sender, EventArgs e) | |
{ | |
this.Activate(); | |
loadLock.Set(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AbpCodeBulider.DxUI | |
{ | |
partial class WaitForm | |
{ | |
/// <summary> | |
/// 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() | |
{ | |
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); | |
this.progressPanel1 = new DevExpress.XtraWaitForm.ProgressPanel(); | |
this.tableLayoutPanel1.SuspendLayout(); | |
this.SuspendLayout(); | |
// | |
// tableLayoutPanel1 | |
// | |
this.tableLayoutPanel1.AutoSize = true; | |
this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; | |
this.tableLayoutPanel1.BackColor = System.Drawing.Color.Transparent; | |
this.tableLayoutPanel1.ColumnCount = 1; | |
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); | |
this.tableLayoutPanel1.Controls.Add(this.progressPanel1, 0, 0); | |
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; | |
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); | |
this.tableLayoutPanel1.Name = "tableLayoutPanel1"; | |
this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(0, 14, 0, 14); | |
this.tableLayoutPanel1.RowCount = 1; | |
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); | |
this.tableLayoutPanel1.Size = new System.Drawing.Size(299, 73); | |
this.tableLayoutPanel1.TabIndex = 2; | |
// | |
// progressPanel1 | |
// | |
this.progressPanel1.Appearance.BackColor = System.Drawing.Color.Transparent; | |
this.progressPanel1.Appearance.Options.UseBackColor = true; | |
this.progressPanel1.AppearanceCaption.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); | |
this.progressPanel1.AppearanceCaption.Options.UseFont = true; | |
this.progressPanel1.AppearanceDescription.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F); | |
this.progressPanel1.AppearanceDescription.Options.UseFont = true; | |
this.progressPanel1.AutoHeight = true; | |
this.progressPanel1.Dock = System.Windows.Forms.DockStyle.Fill; | |
this.progressPanel1.ImageHorzOffset = 20; | |
this.progressPanel1.Location = new System.Drawing.Point(0, 17); | |
this.progressPanel1.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3); | |
this.progressPanel1.Name = "progressPanel1"; | |
this.progressPanel1.Size = new System.Drawing.Size(299, 39); | |
this.progressPanel1.TabIndex = 0; | |
this.progressPanel1.Text = "progressPanel1"; | |
// | |
// WaitForm | |
// | |
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(299, 73); | |
this.Controls.Add(this.tableLayoutPanel1); | |
this.DoubleBuffered = true; | |
this.Name = "WaitForm"; | |
this.Text = "WaitForm"; | |
this.Shown += new System.EventHandler(this.WaitForm_Shown); | |
this.tableLayoutPanel1.ResumeLayout(false); | |
this.ResumeLayout(false); | |
this.PerformLayout(); | |
} | |
#endregion | |
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; | |
private DevExpress.XtraWaitForm.ProgressPanel progressPanel1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://gitee.com/liminghui/AbpCodeBulider/blob/master/AbpCodeBulider.DxUI/WaitForm.cs