简体   繁体   English

具有多个列表框控件的多个FileSystem监视程序

[英]Multiple FileSystem watcher with multiple listbox control

I'm trying to make a monitoring page to monitor various filesystem watcher running to do some job. 我正在尝试创建一个监视页面来监视各种正在运行的文件系统监视程序以完成某些工作。 What I need to know is how do you get multiple filesystem watcher's to access the list boxes in the UI thread.. Here is some code: 我需要知道的是如何让多个文件系统观察者访问UI线程中的列表框。这是一些代码:

private void WatchFile(TextBox ctrlTB,ListBox ctrlLB,FileSystemWatcher _watcher)
    {
        // FileSystemWatcher _watcher = new FileSystemWatcher();
        //var localTB = ctrlTB as TextBox;
        //var localLB = ctrlLB as ListBox;
        _watcher.Path = ctrlTB.Text;
        _watcher.Path = ctrlTB.Text; 



        _watcher.NotifyFilter = NotifyFilters.LastWrite;
        _watcher.Filter = "*.xml";

        _watcher.Changed += new FileSystemEventHandler(convertXML);
       // _watcher.Changed += (s, e) => convertXML(s,e); 
       // _watcher.Error += new ErrorEventHandler(WatcherError);
        _watcher.EnableRaisingEvents = true;
        _watcher.IncludeSubdirectories = false;


        ctrlLB.Items.Add("Started Monitoring @ " + ctrlTB.Text);
        ctrlLB.SelectedIndex = ctrlLB.Items.Count - 1;
    }

public void convertXML(object source, FileSystemEventArgs f)
{
  /// some job
}

I need to post back the status for each filesystemwatcher back to it's respective listbox. 我需要将每个filesystemwatcher的状态发回到各自的列表框。 I'm declaring the FSW's at the click of the start button. 单击开始按钮时,将声明FSW。 Every List box has a start button where it would be declared separately. 每个列表框都有一个开始按钮,将在此处分别声明该按钮。 An eg: 例如:

private void button9_Click(object sender, EventArgs e)
    {
        if (!Directory.Exists(this.textBox1.Text))
        {
            //Form2.ActiveForm.Text = "Please select Source Folder";
            // popup.Show("Please Select Source Folder");
            MessageBox.Show("Please Select Proper Source Folder");
            return;
        }

        else
        {
            textBox1.Enabled = false;

            button9.Enabled = false;
            button1.Enabled = false;
          //  button4.Enabled = false;
            FileSystemWatcher _watcher = new FileSystemWatcher();
            _watcher.SynchronizingObject = this;
           WatchFile(textBox1,listBox1 ,_watcher);
        }
    }

How does the thread know which contrl listbox to access. 线程如何知道要访问哪个控制列表框。

Encapsulate your WatchFile and convertXml into its own class like so 像这样封装您的WatchFileconvertXml到自己的类

public class MyFileWatcher {
   private TextBox _textBox;
   private ListBox _listBox;
   FileSystemWatcher _watcher;

   public MyFileWatcher(TextBox textBox, ListBox listBox, ISynchronizeInvoke syncObj) {
       this._textBox = textBox;
       this._listBox = listBox;

       this._watcher = new FileSystemWatcher();
       this._watcher.SynchronizingObject = syncObj;
       this._watcher.Path = textBox.Text;
       this._watcher.Changed += new FileSystemEventHandler(convertXML);
       this._watcher.EnableRaisingEvents = true;
       this._watcher.IncludeSubdirectories = false;

       // add any other required initialization of the FileSystemWatcher here. 
   }

   protected virtual void convertXML(object source, FileSystemEventArgs f) {
       // interact with this._textBox and this._listBox here as required.
       // e.g.
       // this._listBox.Items.Add(f.FullPath);
   }
}

This way you can tie the FileSystemWatcher instance to a particular TextBox and ListBox, or any other object you desire. 这样,您可以将FileSystemWatcher实例绑定到特定的TextBox和ListBox或所需的任何其他对象。

Then to replace your button9_Click method: 然后替换您的button9_Click方法:

private void button9_Click(object sender, EventArgs e)
{
    if (!Directory.Exists(this.textBox1.Text))
    {
        //Form2.ActiveForm.Text = "Please select Source Folder";
        // popup.Show("Please Select Source Folder");
        MessageBox.Show("Please Select Proper Source Folder");
        return;
    }

    else
    {
        textBox1.Enabled = false;

        button9.Enabled = false;
        button1.Enabled = false;

        // instantiate your new instance of your MyFileWatcher class.
        MyFileWatcher myWatcher = new MyFileWatcher(textBox1,listBox1 ,this);
    }
}

Note: I have not actually compiled or executed this code so there may be exceptions. 注意:我尚未实际编译或执行此代码,因此可能会有异常。 But the overall pattern should solve what you're after. 但是整体模式应该可以解决您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM