C# 具有FileSystemWatcher的Backgroundworker
我正在尝试实现一个C# 具有FileSystemWatcher的Backgroundworker,c#,multithreading,backgroundworker,filesystemwatcher,C#,Multithreading,Backgroundworker,Filesystemwatcher,我正在尝试实现一个BackgroundWorker来监视文件系统监视程序服务 我的代码分为: 包含所有方法、变量和FileSystemWatcher实现的class.cs。主窗体1包含窗体数据并调用按钮\等。当我运行程序时,所发生的只是光标要更改(这已经是预期的)-操作发生在后台(事情完成了),但我的进度条上没有显示报告。我从一个网站上得到了这个例子,并将它改编成我的代码——我做的有什么不对吗?我相信这与我唯一调用的是filesystemwatcher这一事实有关,但我希望它会根据“在后台”运行
BackgroundWorker
来监视文件系统监视程序
服务
我的代码分为:
包含所有方法、变量和FileSystemWatcher
实现的class.cs。主窗体1包含窗体数据并调用按钮\等。当我运行程序时,所发生的只是光标要更改(这已经是预期的)-操作发生在后台(事情完成了),但我的进度条上没有显示报告。我从一个网站上得到了这个例子,并将它改编成我的代码——我做的有什么不对吗?我相信这与我唯一调用的是filesystemwatcher这一事实有关,但我希望它会根据“在后台”运行的操作报告进度
感谢您的帮助。谢谢
我的form1代码(BackgroundWorker部分)和FileSystemWatcher部分如下所示:
namespace PPF_Converter_v10
{
public partial class Form1 : Form
{
private FileManipulation prg;
//private FileManipulation FileOp;
public Form1()
{
InitializeComponent();
//FileOp = new FileManipulation();
prg = new FileManipulation();
//Load config before the program begins - loading sample config or newly generated config
prg.LoadConfig();
FillTextBox();
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
}
BackgroundWorker代码:
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (!textBox1.Text.Contains("\\"))
{
MessageBox.Show("Please define the input folder before starting");
}
else if (!textBox2.Text.Contains("\\"))
{
MessageBox.Show("Please define the XML Output folder before starting");
}
else if (!textBox3.Text.Contains("\\"))
{
MessageBox.Show("Please define the Converted PPF Output Folder before starting");
}
else if (!textBox4.Text.Contains("\\"))
{
MessageBox.Show("Please define the Invalid PPF Output Folder before starting");
}
else
{
// calls the watcher
// prg.FileWatcher.SynchronizingObject = progressBar1.
prg.ProgramProcessing(textBox1.Text);
}
// do some long-winded process here
// this is executed in a separate thread
int maxOps = 1000000;
for (int i = 0; i < maxOps; i++)
{
//rtbText.AppendText(i.ToString() + "\r\n");
// report progress as a percentage complete
bgWorker.WorkerReportsProgress = true;
bgWorker.ReportProgress(100 * i / maxOps);
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// update the progress bar
pbProgress.Value = e.ProgressPercentage;
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// return to "normal" mode of operation
this.Cursor = Cursors.Default;
btnGo.Enabled = true;
}
private void btnGo_Click_1(object sender, EventArgs e)
{
// give the appearance of something happening
this.Cursor = Cursors.WaitCursor;
btnGo.Enabled = false;
// call RunWorkerAsync to start the background thread
bgWorker.RunWorkerAsync();
}
private void bgWorker\u DoWork(对象发送方,DoWorkEventArgs e)
{
如果(!textBox1.Text.Contains(“\\”)
{
MessageBox.Show(“请在启动前定义输入文件夹”);
}
如果(!textBox2.Text.Contains(“\\”),则为else
{
Show(“请在启动之前定义XML输出文件夹”);
}
如果(!textBox3.Text.Contains(“\\”),则为else
{
MessageBox.Show(“请在启动前定义转换后的PPF输出文件夹”);
}
如果(!textBox4.Text.Contains(“\\”),则为else
{
MessageBox.Show(“请在启动前定义无效的PPF输出文件夹”);
}
其他的
{
//呼叫观察者
//prg.FileWatcher.SynchronizingObject=progressBar1。
程序处理(textBox1.Text);
}
//在这里做一些冗长的过程
//这是在一个单独的线程中执行的
int maxOps=1000000;
对于(int i=0;i
启用RichtextBox
时引发异常:
其他信息:跨线程操作无效:从创建控件的线程以外的线程访问控件“rtbText”。您正在从前台线程的后台线程调用MessageBox。这就像在两个独立的线程中执行UI一样,这是一个禁忌 您可以使用事件或后台线程中的。我可能会选择后者。这样,当出现问题时,后台线程可以(也应该)立即中止,并通过消息通知它无法处理文件 把后台任务想象成没有UI的任务。它位于后台,只能使用事件或消息与UI线程通信