后台工作者类并使用来自 c# 中不同类的进度事件传递消息
因此,我有一个类在新的后台工作人员中启动一个新类,并且后台工作人员使用 Progresschanged 部分将状态消息传回。
输入来使用它时,
classname.Dataworker.reportprogress(5)
当我尝试通过从单独的类中
我收到一个错误,表明我在定义之前使用了一个对象。我发现的示例都使用单个类和其中的不同函数。
这可能是一个愚蠢的简单错误,但我就是看不到它,感谢您提供的任何帮助!
我的代码的总体概述是:
//form class
public static BackgroundWorker bw = new BackgroundWorker();
onbuttonclick
{
installer install = new installer();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += class2.aFunction;
bw.ProgressChanged += new ProgressChangedEventHandler(mainForm_InstallerEvent);
bw.RunWorkerAsync();
}
private void mainForm_InstallerEvent(object sender, ProgressChangedEventArgs e)
{
lbl.Text = e.UserState.ToString();
}
////class2 后台工作者类
aFunction
{
InstallerForm.bw.ReportProgress(5); //errors on this!
}
So i have one class which starts a new class in a new background worker, and the background worker passes status messages back using the progresschanged section.
When i try and and use this by typing
classname.Dataworker.reportprogress(5)
from a seperate class i get an error that I am using an object before definition.
The examples I have found all use a single class and different functions within that.
It may be a stupid easy mistake but i just can't see it, thanks for any help you can give!
A general overview of my code is:
//form class
public static BackgroundWorker bw = new BackgroundWorker();
onbuttonclick
{
installer install = new installer();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += class2.aFunction;
bw.ProgressChanged += new ProgressChangedEventHandler(mainForm_InstallerEvent);
bw.RunWorkerAsync();
}
private void mainForm_InstallerEvent(object sender, ProgressChangedEventArgs e)
{
lbl.Text = e.UserState.ToString();
}
////class2 the background worker class
aFunction
{
InstallerForm.bw.ReportProgress(5); //errors on this!
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想做某事,您需要使用 UserState 调用 ReportProgress像这样:
然后你的调用看起来像这样:
现在看起来你的 e.UserState 将为 null,并且调用 ToString() 将导致空引用异常。
这里是一个 UserState 为文本的示例。
You will need to call ReportProgress with a UserState if you want to do something like this:
Then your call would look something like this:
Right now it looks like your e.UserState will be null, and calling ToString() will cause a null reference exception.
Here is an example where the UserState is text.
感谢您的帮助,在这些答案和我发现我已经设法使其工作的答案之间,我缺少的一行是:
后台工作人员 = (后台工作人员)发送者;
然后引用该工作对象
worker.reportprogress(..)
我发现有用的指南是: http://www .nerdparadise.com/tech/coding/csharp/backgroundworker/
完美,谢谢大家:)
Thanks for the help, between those answers and one I found I've managed to get it work, the line I was missing is:
BackgroundWorker worker = (BackgroundWorker)sender;
and then reference that worker object with
worker.reportprogress(..)
The guide i found useful is: http://www.nerdparadise.com/tech/coding/csharp/backgroundworker/
perfect, thanks guys :)
ReportProgress 由后台工作线程用来将百分比值传递给 Progress_Changed 委托。这篇文章向您展示了同步和异步示例。
ReportProgress is used by the background worker thread to pass a percentage value to the Progress_Changed delegate(s). This article shows you examples both synchronous and asynchronous.
到目前为止,这是我的懒惰解决方法(因为我不想使用额外的事件处理程序)。当时我也不想理解
userstate
;)因此我使用了一个包含所有警报/消息的列表来执行特定的长时间操作。消息字符串保存在应用程序内强大的资源存储库App.Properties.Settings
中。由于ReportProgress
采用整数,因此我将ReportProgress
中的列表索引发送到Progress_Changed
。例子:
do_work 中调用了以下方法。
This was my lazy workaround (because I didn't want to use an extra event handler) so far. At the that time I also didn't want to understand the
userstate
;) So I used a list with all the alerts/messages for a particular long operation. Message strings were saved inApp.Properties.Settings
, the mighty resource repository within the app. SinceReportProgress
is taking an integer, I send list index withinReportProgress
toProgress_Changed
.example:
Following method was called in do_work.