【问题标题】:Multiple FileSystem watcher with multiple listbox control具有多个列表框控件的多个 FileSystem 观察程序
【发布时间】:2023-03-20 09:28:01
【问题描述】:

我正在尝试制作一个监控页面来监控各种运行的文件系统观察程序以完成某些工作。我需要知道的是如何让多个文件系统观察者访问 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
}

我需要将每个文件系统观察器的状态回发到它各自的列表框中。我在单击开始按钮时声明 FSW。每个列表框都有一个开始按钮,可以在其中单独声明。例如:

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);
        }
    }

线程如何知道要访问哪个控制列表框。

【问题讨论】:

    标签: c# .net winforms filesystemwatcher


    【解决方案1】:

    Encapsulate 你的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);
       }
    }
    

    通过这种方式,您可以将 FileSystemWatcher 实例绑定到特定的 TextBox 和 ListBox,或您想要的任何其他对象。

    然后替换你的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);
        }
    }
    

    注意:我还没有实际编译或执行此代码,因此可能存在异常。但是整体模式应该可以解决您的需求。

    【讨论】:

    • 这看起来不错,而且可以理解。让我试试这个,我会尽快回复你。谢谢。
    • 别担心,伙计。很高兴能帮上忙。
    • 我收到一个错误:'无法将类型'object'隐式转换为'System.ComponentModel.ISynchronizeInvoke'。存在显式转换(您是否缺少演员表?)'
    • 此错误出现在:“this._watcher.SynchronizingObject = syncObj;”
    • 在 MyFileWatcher 的构造函数中,将 syncObj 参数的类型从 Object 更改为 System.ComponentModel.ISynchronizeInvoke。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多