背景和进步 [英] backgroundwork and progress

查看:55
本文介绍了背景和进步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难把头包裹在背景工作者周围。类和

更新调用它的窗体。


i有一些关于如何更新窗体控件的问题。

i有多个标签,我正在使用文本框进行状态更新。

任务完成时标签会发生变化。

文本框报告正在运行的任务。


所以

1)当我调用_ProgressChange时我需要该函数来传递标签i

想要更新和什么消息?

2)_ProgressChange是否知道如何更新标签和文本框或者我必须定义其他参数以将窗体控件链接到

_ProgressChange。

3)如果我有很多功能,彼此开始并且他们给出状态

我需要一个不同的背景工作者吗?每个功能?或者他们可以将b $ b绑定到一个帖子吗? (每个功能继续在第一个完成之前完成

下一个开始)。

任何人或有任何人遇到一个很好的复杂样本

" backgroundwork"有多个_ProgressChange的类?

解决方案

5月16日下午1:26,auldh< au ... @ discussion.microsoft .comwrote:


我很难绕过背景工作者。类和

更新调用它的窗体。


i有一些关于如何更新窗体控件的问题。

i有多个标签,我正在使用文本框进行状态更新。

任务完成时标签会发生变化。

文本框报告正在运行的任务。


所以

1)当我调用_ProgressChange时我需要该函数来传递标签i

想要更新和什么消息?

2)_ProgressChange是否知道如何更新标签和文本框或者我必须定义其他参数以将窗体控件链接到

_ProgressChange。

3)如果我有很多功能,彼此开始并且他们给出状态

我需要一个不同的背景工作者吗?每个功能?或者他们可以将b $ b绑定到一个帖子吗? (每个功能继续在第一个完成之前完成

下一个开始)。


任何人或有任何人遇到一个很好的复杂样本

" backgroundwork"有多个_ProgressChange的类?



你不能调用objectName_ProductChanged方法。您在后台工作程序上调用

实例方法ReportProgress。那个方法

处理线程切换并在适当的线程上提升_ProgressChanged

事件。


所以你会有:


private void bworker1_DoWork(object sender,DoWorkEventArgs e){

BackgroundWorker worker;


worker =(BackgroundWorker )发件人;


for(int i = 0; i< 10; i + = 1){

worker.ReportProgress(i,null);

}

}


private void bworker1_ProgressChanged(object sender,

ProgressChangedEventArgs e){

myProgressBar.Value = e.ProgressPercentage * 10;

}


DoWork发生在后台线程上;所有其他事件处理程序

BackgroundWorker出现在UI线程上。


HTH

Andy


2008年5月16日星期五10:26:01 -0700,auldh

< au *** @ discussion.microsoft.comwrote:


我很难绕过背景工作者。 class



更新调用它的窗体。



如果你第一次完全理解如何使用Invoke()将会有所帮助

缺少像BackgroundWorker这样的类。 BackgroundWorker所做的大部分工作都是封装对Invoke()的调用,所以你不必处理它。如果

你不理解Invoke(),那么很难理解BackgroundWorker。


i有一些关于如何更新Windows窗体控件。

i有多个标签,我正在使用文本框进行状态更新。

任务完成时标签会发生变化。

文本框报告正在运行的任务。


所以

1)当我调用_ProgressChange时我需要该函数来传递标签i

想要更新和什么消息?



什么是_ProgressChange?你怎么称呼它?在BackgroundWorker类的

中没有这样的方法,因此很难知道你在讨论什么是b $ b。


如果你的意思是BackgroundWorker.ReportProgress(),它会引发

ProgressChanged事件,那么ReportProgress()的一次重载需要一个

对象作为一个参数。这将复制到

ProgressChangedEventArgs.UserState属性。所以你可以传递你想要更新的标签

,或者更复杂的类,它引用Label和

消息字符串以及你想要的任何其他内容。
< blockquote class =post_quotes>
2)_ProgressChange是否知道如何更新标签和文本框或者我必须定义其他参数以将窗体控件链接到

_ProgressChange。



再次,什么是'_ProgressChange'?


BackgroundWorker类对你的一无所知自己的UI。它知道的所有

是将事件ProgressChanged和RunWorkerCompleted

编组回到创建BackgroundWorker的线程(如果

可能.. 。如果您在主GUI中的Forms应用程序中创建它

线程,这是BackgroundWorker类的常用用法,那么它将是可能的b
。在提升

ProgressChanged和RunWorkerCompleted事件时,它基本上是为你调用Invoke(),这样你就不必了。


你必须写所有实际更新UI的代码。那个代码

将放在你的ProgressChanged事件处理程序中,所以当然你需要

来确保所需的所有信息都是在

适当的时候,当你调用BackgroundWorker.ReportProgress()时。


3)如果我有很多函数相互启动并且它们给出

状态

我需要一个不同的背景工作者吗?每个功能?或者他们可以将b
$ b绑定到一个帖子吗? (每个功能继续在第一个完成

之前

下一个开始)。



由于BackgroundWorker根本不知道你如何显示进度,你可以用任何方式做到这一点你喜欢。如果你想要从同一个

BackgroundWorker的同一个DoWork事件中调用多个

方法,只需从一个方法中调用这些方法即

实际上是DoWork事件处理程序。


是否有任何人或有任何人遇到一个好的复杂样本

" backgroundwork"有多个_ProgressChange的类?



MSDN有使用BackgroundWorker的示例代码。我建议您查看

并充分了解示例代码,以便您可以在不使用错误术语的情况下讨论使用

BackgroundWorker。一旦你使用

一切正确的术语和拼写,就会更容易理解你的问题。


Pete


谢谢Andy。

但我的问题怎么样?


"安迪"写道:


5月16日下午1:26,auldh< au ... @ discussion.microsoft.comwrote:


我很难把头包裹起来背景工作者类和

更新调用它的窗体。


i有一些关于如何更新窗体控件的问题。

i有多个标签,我正在使用文本框进行状态更新。

任务完成时标签会发生变化。

文本框报告正在运行的任务。


所以

1)当我调用_ProgressChange时我需要该函数来传递标签i

想要更新和什么消息?

2)_ProgressChange是否知道如何更新标签和文本框或者我必须定义其他参数以将窗体控件链接到

_ProgressChange。

3)如果我有很多功能,彼此开始并且他们给出状态

我需要一个不同的背景工作者吗?每个功能?或者他们可以将b $ b绑定到一个帖子吗? (每个功能继续在第一个完成之前完成

下一个开始)。


任何人或有任何人遇到一个很好的复杂样本

" backgroundwork"有多个_ProgressChange的类?



你不能调用你的objectName_ProductChanged方法。您在后台工作程序上调用

实例方法ReportProgress。那个方法

处理线程切换并在适当的线程上提升_ProgressChanged

事件。


所以你会有:


private void bworker1_DoWork(object sender,DoWorkEventArgs e){

BackgroundWorker worker;


worker =(BackgroundWorker )发件人;


for(int i = 0; i< 10; i + = 1){

worker.ReportProgress(i,null);

}

}


private void bworker1_ProgressChanged(object sender,

ProgressChangedEventArgs e){

myProgressBar.Value = e.ProgressPercentage * 10;

}


DoWork发生在后台线程上;所有其他事件处理程序

BackgroundWorker出现在UI线程上。


HTH

Andy


am having a hard time wrapping my head around "backgroundworker" class and
updates to the window form that calls it.

i have some questions about how to update windows form controls.
i have multiple labels and i''m using a text box for status updates.
the labels change when a task completes.
the text box report what task is running.

so
1) when i call _ProgressChange i need that function to passed the label i
want updating and what message?
2) does _ProgressChange know how to update the label and text box or do i
have to defind other parameters to link the windows form control to the
_ProgressChange.
3) if i have many functions that start after each other and they give status
do i need a different "backgroundworker" for each function? or can they be
tied to one thread? (each function relays on the first one to finish before
the next one starts).
does anyone or has anyone come across a good complex sample of a
"backgroundwork" class with multiple _ProgressChange?

解决方案

On May 16, 1:26 pm, auldh <au...@discussions.microsoft.comwrote:

am having a hard time wrapping my head around "backgroundworker" class and
updates to the window form that calls it.

i have some questions about how to update windows form controls.
i have multiple labels and i''m using a text box for status updates.
the labels change when a task completes.
the text box report what task is running.

so
1) when i call _ProgressChange i need that function to passed the label i
want updating and what message?
2) does _ProgressChange know how to update the label and text box or do i
have to defind other parameters to link the windows form control to the
_ProgressChange.
3) if i have many functions that start after each other and they give status
do i need a different "backgroundworker" for each function? or can they be
tied to one thread? (each function relays on the first one to finish before
the next one starts).

does anyone or has anyone come across a good complex sample of a
"backgroundwork" class with multiple _ProgressChange?

You don''t call your objectName_ProductChanged method. You call the
instance method ReportProgress on the background worker. That method
handles doing the thread switching and raising the _ProgressChanged
event on the proper thread.

So you would have:

private void bworker1_DoWork( object sender, DoWorkEventArgs e ) {
BackgroundWorker worker;

worker = (BackgroundWorker)sender;

for ( int i = 0 ; i < 10 ; i += 1 ) {
worker.ReportProgress( i, null );
}
}

private void bworker1_ProgressChanged( object sender,
ProgressChangedEventArgs e ) {
myProgressBar.Value = e.ProgressPercentage * 10;
}

DoWork occurs on the background thread; all other event handlers for
the BackgroundWorker occur on the UI thread.

HTH
Andy


On Fri, 16 May 2008 10:26:01 -0700, auldh
<au***@discussions.microsoft.comwrote:

am having a hard time wrapping my head around "backgroundworker" class
and
updates to the window form that calls it.

It will be helpful if you first fully comprehend how Invoke() is used
absent a class like BackgroundWorker. Much of what BackgroundWorker does
is encapsulate the call to Invoke() so you don''t have to deal with it. If
you don''t understand Invoke(), it''s hard to understand BackgroundWorker.

i have some questions about how to update windows form controls.
i have multiple labels and i''m using a text box for status updates.
the labels change when a task completes.
the text box report what task is running.

so
1) when i call _ProgressChange i need that function to passed the label i
want updating and what message?

What is "_ProgressChange"? How do you call it? There''s no such method in
the BackgroundWorker class, so it''s very difficult to know what you''re
talking about.

If you really mean BackgroundWorker.ReportProgress(), which raises the
ProgressChanged event, then one overload of ReportProgress() takes an
Object as a parameter. This is copied to the
ProgressChangedEventArgs.UserState property. So you could pass the Label
you want updating, or a more complex class that references the Label and a
message string and anything else you want.

2) does _ProgressChange know how to update the label and text box or do i
have to defind other parameters to link the windows form control to the
_ProgressChange.

Again, what''s "_ProgressChange"?

The BackgroundWorker class doesn''t know anything about your own UI. All
it knows is to marshal the events ProgressChanged and RunWorkerCompleted
back to the thread on which the BackgroundWorker was created (if
possible...if you create it in a Forms application from the main GUI
thread, which is the usual use of the BackgroundWorker class, then it will
be possible). It is essentially calling Invoke() for you when raising the
ProgressChanged and RunWorkerCompleted events so that you don''t have to.

You have to write all of the code to actually update your UI. That code
would go in your ProgressChanged event handler, and so of course you need
to make sure that all of the information that will be needed is passed at
the appropriate time, when you call BackgroundWorker.ReportProgress().

3) if i have many functions that start after each other and they give
status
do i need a different "backgroundworker" for each function? or can they
be
tied to one thread? (each function relays on the first one to finish
before
the next one starts).

Since the BackgroundWorker doesn''t know anything at all about how you''re
displaying progress, you can do it any way you like. If you want multiple
methods to be called from the same DoWork event of the same
BackgroundWorker, just call those methods from a single method that is
actually the DoWork event handler.

does anyone or has anyone come across a good complex sample of a
"backgroundwork" class with multiple _ProgressChange?

MSDN has sample code using BackgroundWorker. I recommend that you review
and learn that sample code well enough that you can discuss your use of
BackgroundWorker without using the wrong terminology. Once you''re using
the correct terms and spelling for everything, it will be a lot easier to
understand your questions.

Pete


thanks Andy.
but what about the questions i have?

"Andy" wrote:

On May 16, 1:26 pm, auldh <au...@discussions.microsoft.comwrote:

am having a hard time wrapping my head around "backgroundworker" class and
updates to the window form that calls it.

i have some questions about how to update windows form controls.
i have multiple labels and i''m using a text box for status updates.
the labels change when a task completes.
the text box report what task is running.

so
1) when i call _ProgressChange i need that function to passed the label i
want updating and what message?
2) does _ProgressChange know how to update the label and text box or do i
have to defind other parameters to link the windows form control to the
_ProgressChange.
3) if i have many functions that start after each other and they give status
do i need a different "backgroundworker" for each function? or can they be
tied to one thread? (each function relays on the first one to finish before
the next one starts).

does anyone or has anyone come across a good complex sample of a
"backgroundwork" class with multiple _ProgressChange?


You don''t call your objectName_ProductChanged method. You call the
instance method ReportProgress on the background worker. That method
handles doing the thread switching and raising the _ProgressChanged
event on the proper thread.

So you would have:

private void bworker1_DoWork( object sender, DoWorkEventArgs e ) {
BackgroundWorker worker;

worker = (BackgroundWorker)sender;

for ( int i = 0 ; i < 10 ; i += 1 ) {
worker.ReportProgress( i, null );
}
}

private void bworker1_ProgressChanged( object sender,
ProgressChangedEventArgs e ) {
myProgressBar.Value = e.ProgressPercentage * 10;
}

DoWork occurs on the background thread; all other event handlers for
the BackgroundWorker occur on the UI thread.

HTH
Andy


这篇关于背景和进步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆