C# 在这种情况下如何使用backgroundworker?
我有一个关于后台工作人员实际如何工作的问题。我不太确定它将如何与不同的方法工作 例如,我有以下代码(为了便于说明而改编自):C# 在这种情况下如何使用backgroundworker?,c#,winforms,user-interface,datatable,backgroundworker,C#,Winforms,User Interface,Datatable,Backgroundworker,我有一个关于后台工作人员实际如何工作的问题。我不太确定它将如何与不同的方法工作 例如,我有以下代码(为了便于说明而改编自): private void getDateTime() { DateTime startTime=DateTime.Now; 双值=数学E; while(DateTime.Now
private void getDateTime()
{
DateTime startTime=DateTime.Now;
双值=数学E;
while(DateTime.Now
//make sure background worker report progress
backgroundWorker1.WorkerReportsProgress = True;
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar1.Refresh;
//if your progressbar is on a status strip, you need to refresh the status strip instead
}
我想出了一个相当不雅观的解决方案。但它满足了用户友好性的要求,所以…不管什么可行:p
谢谢你的反馈,我很感激
不管怎样,代码如下:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(1); //to invoke ProgressChanged
t3 = runComparison(); //run intensive method
e.Result = t3; //return DataTable
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Visible = true; //display a label
textBoxProgress.Visible = true; //display the progress bar
//change the progress bar style
//the Marquee style is constantly running, so it could show users the process is working
//not too sure how to explain it, you'll have to try it out.
progressBar1.Style = ProgressBarStyle.Marquee;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//reset the display and update the DataGridView
progressBar1.Visible = false;
textBoxProgress.Visible = false;
progressBar1.Style = ProgressBarStyle.Blocks;
dataGridView1.DataSource = e.Result;
}
}
不要使用for循环来解析数据表,在BackgroundWorker调用的方法中解析数据表并返回最终结果。@jags我已经在使用for循环来解析方法中的数据表。如果我在BackgroundWorker中使用for循环来获得进度,我将在DataTable方法中反复执行for循环增益。GetDataTable方法是否与getDateTime方法有关?如果不是,为什么不使用其他backgroundworker?如果是,请尝试在for循环之前调用GetDataTable?您不必使用单独的\extra for循环来报告进度。您可以使用用于分析数据选项卡的相同for循环来报告进度如果您提供runComparison()方法实现,也许我们可以帮助您完成报告过程。
//make sure background worker report progress
backgroundWorker1.WorkerReportsProgress = True;
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar1.Refresh;
//if your progressbar is on a status strip, you need to refresh the status strip instead
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(1); //to invoke ProgressChanged
t3 = runComparison(); //run intensive method
e.Result = t3; //return DataTable
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Visible = true; //display a label
textBoxProgress.Visible = true; //display the progress bar
//change the progress bar style
//the Marquee style is constantly running, so it could show users the process is working
//not too sure how to explain it, you'll have to try it out.
progressBar1.Style = ProgressBarStyle.Marquee;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//reset the display and update the DataGridView
progressBar1.Visible = false;
textBoxProgress.Visible = false;
progressBar1.Style = ProgressBarStyle.Blocks;
dataGridView1.DataSource = e.Result;
}
}