2014-09-25 3 views
0

여러 FilesystemWatcher 인스턴스가있어 여러 폴더를 모니터링하고 관련 목록 상자를 업데이트하는 C# 응용 프로그램을 만들고 있습니다. 이 해고되어 지금 때FilesystemWatcher 이벤트를 한 번만 실행합니다.

public 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); 
      //object syncobj = Form1.ActiveForm; 
      string destfolder = textBox7.Text + "\\"; 
      destfolder += "test.xml"; 
      MyFileWatcher myWatcher = new MyFileWatcher(textBox1, listBox1, destfolder, this); 

      myWatcher.WatchFile(textBox1, listBox1); 

     } 
    } 

:

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


    public MyFileWatcher(TextBox textBox, ListBox listBox, string destfolderTextBox , System.ComponentModel.ISynchronizeInvoke syncObj) 
    { 
     this._textBox = textBox; 
     this._listBox = listBox; 
    this._folderDestination = destfolderTextBox; 

     this._watcher = new FileSystemWatcher(); 
     // this._watcher.SynchronizingObject = syncObj; 
     this._watcher.Changed += new FileSystemEventHandler(convertXML); 

     this._watcher.IncludeSubdirectories = false; 
     this._watcher.Path = textBox.Text; 
     this._watcher.EnableRaisingEvents = true; 

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

    } 

    public void WatchFile(TextBox ctrlTB, ListBox ctrlLB) 
    { 
     // 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; 
    } 

은 발사된다. 그것은 처음으로 작동하지만 두 번째 픽업을하지 않습니다. _watcher 인스턴스가 가비지 수집되는 것 같습니다. 하지만 내가 이해할 수없는 점은 MyFileWatcher 클래스 내에 전역으로 선언되어 있다는 것입니다. 이 이후

+2

'button9_Click'에 로컬 변수'myWatcher'를 생성하고 있습니다. (예,'MyFileWatcher'는 아마도 가비지 수집 될 것입니다.). 그 대신에'MyFileWatcher'를 필드로 추가하면 어떻게됩니까? – Default

+0

가능한 중복 [FileSystemWatcher - 한 번만 발사 된 변경 이벤트?] (http://stackoverflow.com/questions/1313942/filesystemwatcher-only-the-change-event-once-firing-once) – Reniuz

+0

하지만 'myWatcher' 수집 할 수있는 지역 변수입니다. 'IDisposable'을 구현하지는 않았지만, 결국'FileSystemWatcher'도 수집 될 것입니다. – Dirk

답변

2
MyFileWatcher myWatcher = new MyFileWatcher(textBox1, listBox1, destfolder, this); 

는 지역 변수이며 MyFileWatcher는 예 garbagecollected 될 FileSystemWatcher을 소유하고 있습니다.

예를 들어 필드로 myWatcher을 추가하면 정상적으로 작동합니다.

+0

감사합니다. 각 인스턴스에 대해 myWatcher 필드를 선언해야합니까? 적어도 7 개의 인스턴스가 동시에 실행되기를 기대합니다. – user726720

+0

다른 경로를 모니터링하고 싶습니까? 예. 이 경우 List 를 유지해야 할 것입니다. 정확한 요구 사항을 모른 채 말하기가 어렵습니다. – Default

관련 문제