C# 在BackgroundWorker循环中使用方法时更新进度条
首先,这是我的代码布局:C# 在BackgroundWorker循环中使用方法时更新进度条,c#,multithreading,visual-studio,progress-bar,backgroundworker,C#,Multithreading,Visual Studio,Progress Bar,Backgroundworker,首先,这是我的代码布局: public partial class GUI : Form { public GUI() { InitializeComponent(); } private void GoButton_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } private void back
public partial class GUI : Form
{
public GUI()
{
InitializeComponent();
}
private void GoButton_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BigMethod bigMeth = new BigMethod();
bigMeth.begin();
}
public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
progressBar.Refresh();
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Done");
}
}
public class BigMethod()
// If it matters, BigMethod() is in a separate .cs file in the same namespace
{
public void begin()
{
for (; time <= 3600; time += T_STEP) // Running for 1 hour
{
//**********************************************
// DO FOR EACH TIMESTEP
//**********************************************
// Stuff
//**********************************************
// Report back
//**********************************************
gui.backgroundWorker1.ReportProgress((int)(time / RUNTIME * 100));
//gui.progressBar.Value = (int)(time / RUNTIME * 100);
Thread.Sleep(100);
timeCounter += T_STEP;
} // End Loop
}
}
public部分类GUI:Form
{
公共图形用户界面()
{
初始化组件();
}
私有void GoButton_Click(对象发送方,事件参数e)
{
backgroundWorker1.RunWorkerAsync();
}
私有void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
{
BigMethod bigMeth=新的BigMethod();
bigMeth.begin();
}
public void backgroundWorker1\u ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
progressBar.Value=e.ProgressPercentage;
progressBar.Refresh();
}
void backgroundWorker1\u RunWorkerCompleted(对象发送方,runworkercompletedeventarge)
{
MessageBox.Show(“完成”);
}
}
公共类BigMethod()
//如果重要的话,BigMethod()位于同一命名空间中的一个单独的.cs文件中
{
公共空间开始()
{
对于(;time而言,backgroundWorker
与GUI线程位于不同的线程上。您必须通过查看invokererequired
()来检查您是否位于GUI线程上。如果不在,请通过调用invoke()
()来调用GUI线程上的函数
这个代码片段应该可以做到这一点
public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<object, ProgressChangedEventArgs>(backgroundWork1_ProgressChanged), sender, e);
return;
}
this.progressBar.Value = e.ProgressPercentage;
}
public void backgroundWorker1\u ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
if(this.invokererequired)
{
调用(新操作(backgroundWork1\u ProgressChanged),发送方,e);
返回;
}
this.progressBar.Value=e.ProgressPercentage;
}
那么您正在将backgroundWorker1
传递到BigMethod
函数中?您可以发布循环(或精简版本)吗调用ReportProgress
?我最初的猜测是你调用它太频繁了,它没有时间去更新一个简单的快速测试,看看它是否被调用太频繁了,就是在giant loop start
中加一个sleep。所以只要添加Thread.sleep(2000)
,看看它是否更新了progress添加了一个线程。sleep(2000)似乎对绘图没有影响。值仍在正确更新。我添加了循环的内容。基本上,它是一个巨大的for()循环,在每次增量时都会报告。您能演示如何将gui变量传递给BigMethod吗?我尝试了您的示例,并将backgroundWorker1传递给begin方法public void begin(System.ComponentModel.BackgroundWorker BackgroundWorker 1)并且工作正常