如何将此类的CountValue更改报告给后台工作者
class SomeOtherClass
{
public void CountUp()
{
int CountValue;
for (int i = 0; i < 100000000; i++)
CountValue = i;
}
}
以下是DoWork功能的实现
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
SomeOtherClass MyOtherClass = new SomeOtherClass();
int CountValue;
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
MyOtherClass.CountUp();
worker.ReportProgress(CountValue);
}
}
答案 0 :(得分:2)
现在看起来SomeOtherClass看起来并没有明显的方法。如果您可以更改SomeOtherClass,则可以添加一个事件:
class CountEventArgs : EventArgs
{
public int CountValue { get; private set; }
public CountEventArgs (int countValue)
{
CountValue = countValue;
}
}
class SomeOtherClass
{
public event EventHandler<CountEventArgs> CountValueChanged;
public void CountUp()
{
int CountValue;
for (int i = 0; i < 100000000; i++)
{
CountValue = i;
OnCountValueChanged(new CountEventArgs(CountValue));
}
}
private void OnCountValueChanged(CountEventArgs e)
{
EventHandler<CountEventArgs> temp = CountValueChanged;
if (temp != null)
{
temp(this, e);
}
}
}
然后,您可以设置事件处理程序并使用BackgroundWorker的ReportProgress方法将信息中继到UI:
BackgroundWorker worker = sender as BackgroundWorker;
SomeOtherClass MyOtherClass = new SomeOtherClass();
// set up an anonymous method as event handler for the CountValueChanged
// event. This event handler passes the count value on to the ReportProgress
// method of the background worker, which in turn will raise the ProgressChanged
// event on the UI thread.
MyOtherClass.CountValueChanged += (eventSender, eventArgs) =>
{
worker.ReportProgress(eventArgs.CountValue);
};
MyOtherClass.CountUp();
答案 1 :(得分:1)
您的代码中出现了很多错误。特别是,当您每秒向其发送数百万次新事件时,您不能指望Windows能够跟上。尝试仅在完成大量工作后发送更新。此外,ReportProgress的参数应为百分比(0 - 100)。如果要发送百分比以外的数据,请使用用户状态参数。以下是您可以使用的一些代码:
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
private void btnStop_Click(object sender, EventArgs e)
{
backgroundWorker.CancelAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
int countValue = 0;
int max = 100000000;
for (int i = 0; i < max; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
countValue = i;
if (i % 1000000 == 0)
worker.ReportProgress(i / (max / 100), i);
}
worker.ReportProgress(100, max);
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
labelCounter.Text = ((int)e.UserState).ToString();
}
}
}
注意:请记住在设计器中将worker的SupportsProgress和SupportsCancellation设置为true。
答案 2 :(得分:0)
不确定我理解,但我会试一试。为什么不更改CountUp()以返回值?
public int CountUp()
{
int CountValue;
for (int i = 0; i < 100000000; i++)
CountValue = i;
return CountValue;
}
这段代码的目的对我没有意义,所以我不确定我理解你想要完成什么。
答案 3 :(得分:0)
我认为您不了解局部变量的工作原理... CountValue
方法中的CountUp
只会写入CountUp
方法中的局部变量。它不会影响CountUp
方法中的backgroundWorker_DoWork
变量。您必须将CountUp
移至DoWork方法才能生效。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100000000; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
worker.ReportProgress(i);
}
}