问题描述
我有一个有效的解决方案,报告进度和放大器;文本到进度条和应用程序的主窗体上的标签。现在我提出我的工人方法的一类,以他们在多个形式等进行访问。
I have a working solution that reports progress & text to a progress bar and a label on the applications's main form. I have now moved my worker methods to a class to they are accessible across multiple forms etc.
在工人方法 BW.ReportProgress()
的推背的进展和文字BackgroundWorker的在主窗体的语句。
Within the worker methods are BW.ReportProgress()
statements that push back the progress and text to the BackgroundWorker in the main form.
要能够更好地了解这里的文件格式:
To give a better idea here is the file layout:
MainScreen.cs
List repSelected = new List();
XMLandRar xXMLandRar = new XMLandRar();
private void Rarbtn_Click(object sender, EventArgs e)
{
GetReps();
//Run worker
if (!CreateRarBW.IsBusy)
{
CreateRarBW.RunWorkerAsync();
}
}
//Worker
private void CreateRarBW_DoWork(object sender, DoWorkEventArgs e)
{
xXMLandRar.RarFiles(repSelected);
}
//Progress reporting
private void CreateRarBW_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progBar.Value = e.ProgressPercentage;
Statuslbl.Text = e.UserState.ToString();
}
然后我的,它包括所有的工人方法和是推动进步的主要形式,新创建的类。
Then my newly created Class that encompasses all of the worker methods and is to push progress to the main form.
XMLandRar.cs
public class XMLandRar
{
public void RarFiles(List repSelected)
{
int step = 100 / repSelected.Count();
int i = 0;
//Iterate through list and run rar for each
foreach (string rep in repSelected)
{
CreateRarBW.ReportProgress(i, "Raring files for " + rep);
DirectoryExists(rep);
ProcessRunner(rep);
i += step;
CreateRarBW.ReportProgress(i, "Raring files for " + rep);
}
}
}
我遇到的问题是,在 XMLandRar
类CreateRarBW无法识别(显然) - 我怎样才能使一个 ReportProgress
在应用程序的主屏幕中拨打到BW?
The problem I am having is that in the XMLandRar
class the CreateRarBW is not recognised (obviously) - how can I make a ReportProgress
call to the BW in the main screen of the application?
推荐答案
创建在 XMLandRar
类的事件,你可以订阅。
Create an event in your XMLandRar
class which you could subscribe to.
这样的 XMLandRar
类不需要知道或关心UI或进度,只在乎发送,如果有人愿意听的消息。而且还可以有一个以上的用户(比方说,如果你要举报的后台工作和日志,也许)
This way the XMLandRar
class doesn't need to know or care about the UI or progressbar, it only cares about sending a message if anyone would listen. And there can also be more than one subscriber (let's say if you want to report to the background worker and a log, maybe)
例如:
private void Rarbtn_Click(object sender, EventArgs e)
{
GetReps();
//Run worker
if (!CreateRarBW.IsBusy)
{
// This should be done once, maybe in the contructor. Bind to new event.
xXMLandRar.ReportProgress += new EventHandler<XMLandRar.ProgressArgs>(xXMLandRar_ReportProgress);
CreateRarBW.RunWorkerAsync();
}
}
protected void xXMLandRar_ReportProgress(object sender, XMLandRar.ProgressArgs e)
{
// Call the UI backgroundworker
CreateRarBW.ReportProgress(e.Percentage, e.Message);
}
public class XMLandRar
{
// Event handler to bind to for reporting progress
public EventHandler<ProgressArgs> ReportProgress;
// Eventargs to contain information to send to the subscriber
public class ProgressArgs : EventArgs
{
public int Percentage { get; set; }
public string Message { get; set; }
}
public void RarFiles(List repSelected)
{
int step = 100 / repSelected.Count();
int i = 0;
//Iterate through list and run rar for each
foreach (string rep in repSelected)
{
// Report progress if somebody is listening (subscribed)
if (ReportProgress != null)
{
ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Raring files for " + rep });
}
DirectoryExists(rep);
ProcessRunner(rep);
i += step;
// Report progress if somebody is listening (subscribed)
if (ReportProgress != null)
{
ReportProgress(this, new ProgressArgs { Percentage = i, Message = "Raring files for " + rep });
}
}
}
}
这篇关于BackgroundWorker的报告来自外部类的进展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!