C# 在已创建的Progressbar类上执行线程-使用C的Windows应用程序#
我有一个创建progressbar的C# 在已创建的Progressbar类上执行线程-使用C的Windows应用程序#,c#,multithreading,progress-bar,C#,Multithreading,Progress Bar,我有一个创建progressbar的类。但我的问题是,我不想使用应用程序。DoEvents()只显示实际事件。我还注意到,填充记录在执行应用程序时会消耗大量时间。DoEvents()我已经阅读了使用线程的相关内容,但我不明白它是如何工作的,以及如何在创建的类上实现它。谁能帮我这么做 下面是我的代码 class LoadProgressBar { public ProgressBar pBar = null; public Label lblProgress = null;
类。但我的问题是,我不想使用应用程序。DoEvents()
只显示实际事件。我还注意到,填充记录在执行应用程序时会消耗大量时间。DoEvents()
我已经阅读了使用线程的相关内容,但我不明白它是如何工作的,以及如何在创建的类上实现它。谁能帮我这么做
下面是我的代码
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);
...
}
}
类加载进度条
{
公共ProgressBar pBar=null;
公共标签lblProgress=null;
公共标签lblTitle=null;
公共表单createProgressBar(int-maxValue)
{
//创建表单
表单pForm=新表单();
p形式尺寸=新尺寸(260,70);
pForm.FormBorderStyle=FormBorderStyle.None;
pForm.StartPosition=FormStartPosition.CenterScreen;
pForm.TopMost=真;
pForm.ShowInTaskbar=假;
//创建标签控件
this.lblTitle=新标签();
this.lblTitle.Location=新点(5,5);
this.lblTitle.Size=新尺寸(250,20);
this.lblTitle.Text=“处理”;
//创建进度条控件
this.pBar=newprogressbar();
this.pBar.Size=新尺寸(250,20);
this.pBar.max=最大值;
此.pBar.Minimum=0;
this.pBar.Location=新点(5,25);
//创建标签控件
this.lblProgress=新标签();
this.lblProgress.Size=新尺寸(250,15);
this.lblProgress.Location=新点(5,50);
//在窗体上添加控件
pForm.Controls.Add(this.pBar);
pForm.Controls.Add(this.lblProgress);
pForm.Controls.Add(this.lblTitle);
返回pForm;
}
公共无效覆盖maxValue(int-maxValue)
{
this.pBar.max=最大值;
}
public void upldateProgress(int步,字符串标题)
{
如果(this.pBar.Value+step)读了一个例子,我已经读过了。到目前为止,我还不能在我的应用程序上运行它。这是什么意思?为什么你的代码是VB和C的混合体?另外,链接中所述的示例不起作用。我错过了静态void main。它已经更改了。