2014-09-23 47 views
1

我想做一个监控页面来监控各种文件系统观察者运行做一些工作。我需要知道的是,你得到多个文件系统观察者的访问列表框在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 
} 

我需要回来后每个FileSystemWatcher的状态返回到它的相应的列表框。我正在点击开始按钮来声明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); 
     } 
    } 

线程如何知道要访问哪个控件列表框。非常感谢答复。

回答

1

EncapsulateWatchFileconvertXml到自己的类,像这样

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的实例特定的文本框和列表框,或任何你想要的其他对象。

然后更换您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); 
    } 
} 

注:我还没有实际编译或执行该代码,所以也有例外。但整体模式应该解决你之后的问题。

+0

这看起来不错,可以理解。让我试试这个,我会很快回复你。谢谢。 – user726720 2014-09-23 15:32:27

+0

+1以便说明。 – user726720 2014-09-23 15:32:52

+0

不用担心队友。很高兴有帮助。 – Adrian 2014-09-23 23:22:36