我有Class
创建进度条。但我的问题是,我不想Application.DoEvents()
只显示实际事件。我还观察到,填充记录会消耗大量时间Application.DoEvents()
,Threading
但我无法理解它是如何工作的,以及如何在我的 created 上实现它Class
。谁能帮我这样做。
以下是我的代码。
class LoadProgressBar
{
public ProgressBar pBar = null;
public Label lblProgress = null;
public Label lblTitle = null;
public Form createProgressBar(int maxValue)
{
//create form
Form pForm = new Form();
pForm.Size = new Size(260, 70);
pForm.FormBorderStyle = FormBorderStyle.None;
pForm.StartPosition = FormStartPosition.CenterScreen;
pForm.TopMost = true;
pForm.ShowInTaskbar = false;
//create label control
this.lblTitle = new Label();
this.lblTitle.Location = new Point(5, 5);
this.lblTitle.Size = new Size(250, 20);
this.lblTitle.Text = "Processing";
//create progress bar control
this.pBar = new ProgressBar();
this.pBar.Size = new Size(250, 20);
this.pBar.Maximum = maxValue;
this.pBar.Minimum = 0;
this.pBar.Location = new Point(5, 25);
//create label control
this.lblProgress = new Label();
this.lblProgress.Size = new Size(250, 15);
this.lblProgress.Location = new Point(5, 50);
//add controls on form
pForm.Controls.Add(this.pBar);
pForm.Controls.Add(this.lblProgress);
pForm.Controls.Add(this.lblTitle);
return pForm;
}
public void overrideMaxValue(int maxValue)
{
this.pBar.Maximum = maxValue;
}
public void upldateProgress(int step, string caption)
{
if (this.pBar.Value + step <= this.pBar.Maximum)
{
this.pBar.Value += step;
}
this.lblProgress.Text = caption;
Application.DoEvents();
}
}
//Main Sub
static void Main()
{
//create instance of progressbar class
LoadProgressBar progBar = new LoadProgressBar();
//create form for progress bar
Form loadBar = progBar.createProgressBar(0);
List<MasterList> empList = new List<MasterList>();
loadBar.Show(); //show progressbar
progBar.upldateProgress(0, "Reading File Content..."); //update current progress state
empList = readFileContent(); //get the data on csv file
//populate content
foreach (MasterList empInfo in empList)
{
progBar.upldateProgress(1, "Populating : " + empInfo.EmpNo); //update current progress state
list = lvList.Items.Add(empInfo.EmpNo);
list.SubItems.Add(empInfo.LastName);
...
}
}