如何将后台工作者与远程类一起使用
如何将此类的 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);
}
}
How do I report change of the CountValue from this class to a backgroundworker
class SomeOtherClass
{
public void CountUp()
{
int CountValue;
for (int i = 0; i < 100000000; i++)
CountValue = i;
}
}
Here is the implemetation of the DoWork function
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 SomeOtherClass 现在看起来的方式来看,没有明显的方法。如果您可以更改 SomeOtherClass,则可以添加一个事件:
然后您可以设置一个事件处理程序并使用 BackgroundWorker 的 ReportProgress 方法将信息转发到 UI:
In the way that SomeOtherClass looks now there is no obvious way. If you can change SomeOtherClass you could add an event:
Then you can set up an event handler and use the ReportProgress method of the BackgroundWorker to relay the information to the UI:
您的代码中犯了很多错误。特别是,当您每秒向 Windows 发送数百万次新事件时,您不能指望 Windows 能够跟上。仅在完成大量工作后才尝试发送更新。此外,ReportProgress 的参数应该是百分比 (0 - 100)。如果要发送百分比以外的数据,请使用用户状态参数。以下是您可以使用的一些代码:
注意:请记住在设计器中将工作线程的 SupportsProgress 和 SupportsCancellation 设置为 true。
You have made a lot of errors in your code. In particular, you can't expect Windows to keep up when you are sending it a new event millions of times per second. Try only sending an update when you have completed a large block of work. Also, the parameter for ReportProgress should be a percentage (0 - 100). If you want to send data other than a percentage, use the user state parameter. Here is some code you can use:
Note: remember to set the worker's SupportsProgress and SupportsCancellation to true in the designer.
不确定我是否理解,但我会尝试一下。为什么不改变 CountUp() 返回值呢?
这段代码的目的对我来说没有意义,所以我不确定我是否理解您想要完成的任务。
Not sure I understand but I'll give it a shot. Why not change CountUp() to return a value?
The purpose of this code doesn't make sense to me so I'm not sure I understand what you were trying to accomplish.
我认为您不了解局部变量的工作原理...
CountUp
方法中的CountValue
只会写入CountUp
中的局部变量> 方法。它不会影响backgroundWorker_DoWork
方法中的CountUp
变量。您必须将CountUp
移至 DoWork 方法才能使其正常工作。I don't think you understand how local variables work... the
CountValue
in yourCountUp
method will only write to the local variable in yourCountUp
method. It does not affect theCountUp
variable in yourbackgroundWorker_DoWork
method. You would have to moveCountUp
to the DoWork method for it to work.