Skip to content

Instantly share code, notes, and snippets.

@superlucky8848
Created December 20, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superlucky8848/6cf80b266cfb69e7160d9e1682dbf3c7 to your computer and use it in GitHub Desktop.
Save superlucky8848/6cf80b266cfb69e7160d9e1682dbf3c7 to your computer and use it in GitHub Desktop.
Modeled Worker Dialog
namespace soLexiconManager
{
partial class DlgWorker
{
/// <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.components = new System.ComponentModel.Container();
this.lbText = new System.Windows.Forms.Label();
this.pBProgress = new System.Windows.Forms.ProgressBar();
this.lBMessage = new System.Windows.Forms.Label();
this.btCancel = new System.Windows.Forms.Button();
this.bgWDoWork = new System.ComponentModel.BackgroundWorker();
this.timerMessage = new System.Windows.Forms.Timer(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.timerShow = new System.Windows.Forms.Timer(this.components);
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// lbText
//
this.lbText.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbText.Location = new System.Drawing.Point(3, 11);
this.lbText.Name = "lbText";
this.lbText.Size = new System.Drawing.Size(320, 60);
this.lbText.TabIndex = 0;
this.lbText.Text = "Text";
this.lbText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// pBProgress
//
this.pBProgress.Location = new System.Drawing.Point(0, 74);
this.pBProgress.Name = "pBProgress";
this.pBProgress.Size = new System.Drawing.Size(326, 23);
this.pBProgress.TabIndex = 1;
//
// lBMessage
//
this.lBMessage.Location = new System.Drawing.Point(3, 100);
this.lBMessage.Name = "lBMessage";
this.lBMessage.Size = new System.Drawing.Size(320, 47);
this.lBMessage.TabIndex = 2;
this.lBMessage.Text = "Message";
//
// btCancel
//
this.btCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btCancel.Location = new System.Drawing.Point(126, 150);
this.btCancel.Name = "btCancel";
this.btCancel.Size = new System.Drawing.Size(75, 23);
this.btCancel.TabIndex = 4;
this.btCancel.Text = "Cancel";
this.btCancel.UseVisualStyleBackColor = true;
this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
//
// bgWDoWork
//
this.bgWDoWork.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bgWDoWork_ProgressChanged);
this.bgWDoWork.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bgWDoWork_RunWorkerCompleted);
//
// timerMessage
//
this.timerMessage.Interval = 1000;
this.timerMessage.Tick += new System.EventHandler(this.timerMessage_Tick);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.Control;
this.panel1.Controls.Add(this.lbText);
this.panel1.Controls.Add(this.btCancel);
this.panel1.Controls.Add(this.pBProgress);
this.panel1.Controls.Add(this.lBMessage);
this.panel1.Location = new System.Drawing.Point(12, 13);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(326, 176);
this.panel1.TabIndex = 5;
//
// timerShow
//
this.timerShow.Interval = 1000;
this.timerShow.Tick += new System.EventHandler(this.timerShow_Tick);
//
// DlgWorker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.ClientSize = new System.Drawing.Size(352, 201);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "DlgWorker";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "DlgWorker";
this.Load += new System.EventHandler(this.DlgWorker_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label lbText;
private System.Windows.Forms.ProgressBar pBProgress;
private System.Windows.Forms.Label lBMessage;
private System.Windows.Forms.Button btCancel;
private System.ComponentModel.BackgroundWorker bgWDoWork;
private System.Windows.Forms.Timer timerMessage;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Timer timerShow;
}
public partial class DlgWorker : Form
{
#region Custom Variables
private int tick;
private object argumentObject;
public int ShowInterval
{
get { return timerShow.Interval; }
set { timerShow.Interval = value; }
}
#endregion
#region Custom Functions
private string FormatTick(int tickCnt)
{
int ms, s, m, h;
ms = tickCnt % 1000;
tickCnt /= 1000;
s = tickCnt % 60;
tickCnt /= 60;
m = tickCnt % 60;
h = tickCnt /= 60;
return String.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", h, m, s, ms);
}
#endregion
private DlgWorker()
{
InitializeComponent();
}
public DlgWorker(DoWorkEventHandler work, object argument, string text, bool reportProgress, bool supportCancellation) : this()
{
this.bgWDoWork.DoWork += work;
this.bgWDoWork.WorkerSupportsCancellation = supportCancellation;
this.bgWDoWork.WorkerReportsProgress = reportProgress;
this.argumentObject = argument;
btCancel.Enabled = btCancel.Visible = supportCancellation;
if (reportProgress)
{
pBProgress.Style = ProgressBarStyle.Continuous;
}
else
{
pBProgress.Style = ProgressBarStyle.Marquee;
pBProgress.MarqueeAnimationSpeed = 5;
}
lbText.Text = text;
}
public DlgWorker(DoWorkEventHandler work, string text, bool reportProgress, bool supportCancellation)
: this(work, null, text, reportProgress, supportCancellation) { }
public DlgWorker(DoWorkEventHandler work, object argument, string text)
: this(work, argument, text, false, false) { }
public DlgWorker(DoWorkEventHandler work, string text)
: this(work, null, text, false, false) { }
public DlgWorker(DoWorkEventHandler work, object argument)
: this(work, argument, "Please Wait...", false, false) { }
public DlgWorker(DoWorkEventHandler work)
: this(work, null, "Please Wait...", false, false) { }
private void bgWDoWork_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int percent = 0;
int tickCnt = System.Environment.TickCount - tick;
string message = e.UserState as string;
if (message == null) message = "";
if (e.ProgressPercentage < 0 && pBProgress.Style!= ProgressBarStyle.Marquee)
{
pBProgress.Style = ProgressBarStyle.Marquee;
pBProgress.MarqueeAnimationSpeed = 5;
}
if (e.ProgressPercentage > 0 && pBProgress.Style != ProgressBarStyle.Continuous)
{
pBProgress.Style = ProgressBarStyle.Continuous;
}
if(e.ProgressPercentage>=0 && e.ProgressPercentage<=100)
pBProgress.Value = percent = e.ProgressPercentage;
message += "\nTime Elapsed: " + FormatTick(tickCnt);
if (percent > 0 && percent < 100)
{
int tickLeft = tickCnt * (100 - percent) / percent;
message += "\nTime Remaining*: " + FormatTick(tickLeft);
}
lBMessage.Text = message;
}
private void bgWDoWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
this.DialogResult = DialogResult.Abort;
}
else if (e.Cancelled) this.DialogResult = DialogResult.Cancel;
else this.DialogResult = DialogResult.OK;
}
private void DlgWorker_Load(object sender, EventArgs e)
{
bgWDoWork.RunWorkerAsync(argumentObject);
tick = System.Environment.TickCount;
if (bgWDoWork.WorkerReportsProgress) timerMessage.Enabled = false;
else timerMessage.Enabled = true;
this.Opacity = 0.00;
timerShow.Enabled = true;
}
private void timerMessage_Tick(object sender, EventArgs e)
{
int tickCnt = System.Environment.TickCount - tick;
lBMessage.Text = "\nTime elapsed: " + FormatTick(tickCnt);
}
private void btCancel_Click(object sender, EventArgs e)
{
bgWDoWork.CancelAsync();
}
private void timerShow_Tick(object sender, EventArgs e)
{
this.Opacity += 0.10;
if(timerShow.Interval!=100) timerShow.Interval = 100;
if (this.Opacity >= 1.00) timerShow.Enabled = false;
}
}
}
public SomeBasicForm : Form
{
int done, total, errCnt;
//...
//Actural Work Here
private void DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
bool doing = true;
BackgroundWorker worker = sender as BackgroundWorker;
string message;
int tick = 0;
if (worker.WorkerReportsProgress)
{
message = String.Format("{0}/{1} Error:{2}", done, total, errCnt);
worker.ReportProgress(done * 100 / total, message);
tick = System.Environment.TickCount;
}
while(doing)
{
//do some work ...
//report working progress.
if (worker.WorkerReportsProgress && System.Environment.TickCount - tick > 1000)
{
message = String.Format("{0}/{1} Error:{2}", done, total, errCnt);
worker.ReportProgress(done * 100 / total, message);
tick = System.Environment.TickCount;
}
else if (System.Environment.TickCount - tick < 0) tick = System.Environment.TickCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment