2014-09-23 5 views
1

나는 어떤 파일 시스템 감시자가 어떤 일을하는지 감시하기위한 모니터링 페이지를 만들고있다.다중리스트 박스 컨트롤을 가진 다중 FileSystem 감시자

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의 상태를 다시 게시해야합니다 내가 알 필요하면 여러 파일 시스템 감시자의이 UI 스레드에서 목록 상자에 액세스 할 수 ... 여기에 몇 가지 코드받을 수 있나요 어떻게 리스트 박스. 시작 버튼을 클릭하면 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); 
     } 
    } 

스레드는 액세스 할 contrl 목록 상자를 어떻게 알 수 있습니까? 대답에 대해 많은 것을 알고 있습니다.

답변

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 구성 인스턴스를 연결할 수 있습니다이 방법으로.

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

참고 :

는 그런 다음 button9_Click 방법을 대체하는 사실은 컴파일 또는 예외가있을 수 있으므로이 코드를 실행하지 않았습니다. 그러나 전반적인 패턴은 당신이 겪고있는 것을 해결할 것입니다.

+0

이 모양이 좋고 이해할 만합니다. 이걸 시험해 볼게요. 곧 연락 드리겠습니다. 고맙습니다. – user726720

+0

+1 설명이 필요합니다. – user726720

+0

걱정할 필요가 없습니다. 도와 줘서 기뻐. – Adrian