从另一个 class 访问后台工作者
Access background worker from a different class
我在 Windows 表单应用程序中将逻辑从一个 class 分离到另一个。当全部合而为一 class 时,它工作正常,但我现在将逻辑移至它自己的 classes。从另一个 class.
获取 backgroundWorker 时遇到一些问题
我在class一个
public partial class ClassA : Form
BackgroundWorker essentialBgWorker = new BackgroundWorker();
public ClassA()
{
InitializeComponent();
//essentialBgWorker
essentialBgWorker.DoWork += new DoWorkEventHandler(essentialBgWorker_DoWork);
essentialBgWorker.ProgressChanged += new ProgressChangedEventHandler(essentialBgWorker_ProgressChanged);
essentialBgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(essentialBgWorker_RunWorkerCompleted);
essentialBgWorker.WorkerReportsProgress = true;
}
这是一个带有按钮的表单,单击该按钮会将文件复制到另一个目录。
private void copyButton_Click(object sender, EventArgs e)
{
clickedButton = ((Button)sender).Name.ToString();
itemsChanged = ((Button)sender).Text.ToString();
essentialBgWorker.RunWorkerAsync();
}
这将运行后台工作程序:
public void essentialBgWorker_DoWork(object sender, DoWorkEventArgs e)
{
string buttonSender = clickedButton; //copyButton, deleteButton, etc.
switch (buttonSender)
{
case "copyButton":
//this is pseudocode - the important part being that this is where I would call the method from the other class
ClassB.copyDocuments();
break;
case "deleteButton":
//this is pseudocode - the important part being that this is where I would call the method from the other class
ClassB.deleteDocuments();
break;
default:
essentialBgWorker.CancelAsync();
break;
}
}
在我的另一个 class (ClassB
) 中,对于此示例,我有单击按钮时调用的方法(应该处理逻辑)。
public void copyDocuments()
{
ClassA classA = new ClassA();
//
//the logic that handles copying the files
//gets the filecount!
//Report to the background worker
int totalFileCount = fileCount;
int total = totalFileCount; //total things being transferred
for (int i = 0; i <= total; i++) //report those numbers
{
System.Threading.Thread.Sleep(100);
int percents = (i * 100) / total;
classA.essentialBgWorker.ReportProgress(percents, i);
//2 arguments:
//1. procenteges (from 0 t0 100) - i do a calcumation
//2. some current value!
}
//Do the copying here
}
这一行我一直有问题:
classA.essentialBgWorker.ReportProgress(percents, i);
这条线在 在 ClassA 时有效 - 但一旦将其引入 ClassB 就会出现错误:
ClassA.essentialBgWorker' is inaccessible due to it's protection level
只是寻求一些帮助,以正确的方式让它从另一个 class 开火。
您必须向 essentialBgWorker 添加 public 修饰符
public BackgroundWorker essentialBgWorker = new BackgroundWorker();
我在 Windows 表单应用程序中将逻辑从一个 class 分离到另一个。当全部合而为一 class 时,它工作正常,但我现在将逻辑移至它自己的 classes。从另一个 class.
获取 backgroundWorker 时遇到一些问题我在class一个
public partial class ClassA : Form
BackgroundWorker essentialBgWorker = new BackgroundWorker();
public ClassA()
{
InitializeComponent();
//essentialBgWorker
essentialBgWorker.DoWork += new DoWorkEventHandler(essentialBgWorker_DoWork);
essentialBgWorker.ProgressChanged += new ProgressChangedEventHandler(essentialBgWorker_ProgressChanged);
essentialBgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(essentialBgWorker_RunWorkerCompleted);
essentialBgWorker.WorkerReportsProgress = true;
}
这是一个带有按钮的表单,单击该按钮会将文件复制到另一个目录。
private void copyButton_Click(object sender, EventArgs e)
{
clickedButton = ((Button)sender).Name.ToString();
itemsChanged = ((Button)sender).Text.ToString();
essentialBgWorker.RunWorkerAsync();
}
这将运行后台工作程序:
public void essentialBgWorker_DoWork(object sender, DoWorkEventArgs e)
{
string buttonSender = clickedButton; //copyButton, deleteButton, etc.
switch (buttonSender)
{
case "copyButton":
//this is pseudocode - the important part being that this is where I would call the method from the other class
ClassB.copyDocuments();
break;
case "deleteButton":
//this is pseudocode - the important part being that this is where I would call the method from the other class
ClassB.deleteDocuments();
break;
default:
essentialBgWorker.CancelAsync();
break;
}
}
在我的另一个 class (ClassB
) 中,对于此示例,我有单击按钮时调用的方法(应该处理逻辑)。
public void copyDocuments()
{
ClassA classA = new ClassA();
//
//the logic that handles copying the files
//gets the filecount!
//Report to the background worker
int totalFileCount = fileCount;
int total = totalFileCount; //total things being transferred
for (int i = 0; i <= total; i++) //report those numbers
{
System.Threading.Thread.Sleep(100);
int percents = (i * 100) / total;
classA.essentialBgWorker.ReportProgress(percents, i);
//2 arguments:
//1. procenteges (from 0 t0 100) - i do a calcumation
//2. some current value!
}
//Do the copying here
}
这一行我一直有问题:
classA.essentialBgWorker.ReportProgress(percents, i);
这条线在 在 ClassA 时有效 - 但一旦将其引入 ClassB 就会出现错误:
ClassA.essentialBgWorker' is inaccessible due to it's protection level
只是寻求一些帮助,以正确的方式让它从另一个 class 开火。
您必须向 essentialBgWorker 添加 public 修饰符
public BackgroundWorker essentialBgWorker = new BackgroundWorker();