FileSystem Watcher对象引用未设置为该对象的实例

本文关键字:对象 实例 设置 Watcher 对象引用 FileSystem | 更新日期: 2023-09-27 18:24:32

我很难排除这个错误。它以前是有效的,但也许我在某个地方犯了一个错误,因为代码有点长。

这是代码:

public class MyFileWatcher
{
    private TextBox _textBox;
    private ListBox _listBox;
    private string _folderDestination;
    FileSystemWatcher  _watcher;
    private int _interval;
    //Timespan created when interval is set
    private TimeSpan _recentTimeSpan;
    Dictionary<string, DateTime> _lastFileEvent = new Dictionary<string, DateTime>();
    DateTime _current;

    public  MyFileWatcher(TextBox textBox, ListBox listBox, string destfolderTextBox ,DateTime current, System.ComponentModel.ISynchronizeInvoke syncObj)
    {
        this._textBox = textBox;
        this._listBox = listBox;
    this._folderDestination = destfolderTextBox;
    this._current = current;
        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.Error += new ErrorEventHandler(WatcherError);
        // _watcher.Changed += (s, e) => convertXML(s,e); 
        // _watcher.Error += new ErrorEventHandler(WatcherError);
        _watcher.IncludeSubdirectories = false;
        _watcher.EnableRaisingEvents = true;

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

它由以下控制触发启动:

 private void button12_Click(object sender, EventArgs e)
    {
        current = new DateTime();
        if ((!Directory.Exists(this.textBox2.Text)) || (!Directory.Exists(this.textBox7.Text)))
        {
            //Form2.ActiveForm.Text = "Please select Source Folder";
            // popup.Show("Please Select Source Folder");
            MessageBox.Show("Please Select Proper Source Folder");
            return;
        }
        else
        {
            textBox2.Enabled = false;
            button12.Enabled = false;
            button11.Enabled = true;
            button2.Enabled = false;
            button7.Enabled = false;
            textBox7.Enabled = false;
            //  button4.Enabled = false;
            // WatchFile();
            string destfolder = textBox7.Text + "''";
            destfolder += "test.xml";
            MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this);
            myWatcher.WatchFile(textBox2, listBox2);
        }
    }

它工作正常。但当尝试通过以下控件停止时会出现错误:

    private void button11_Click(object sender, EventArgs e)
    {
        // _watcher.EnableRaisingEvents = false;
        //     _watcher.Changed -= new FileSystemEventHandler(InitList);
        //  _watcher.Dispose();
        //((FileSystemWatcher)sender).Dispose();
        listBox2.Items.Add("Stopped Monitoring Directory ");
        listBox2.SelectedIndex = listBox2.Items.Count - 1;
        textBox2.Enabled = true;
        button10.Enabled = true;
        button2.Enabled = true;
        button7.Enabled = true;
        textBox7.Enabled = true;
       // if (myWatcher != null)
            myWatcher.RemoveWatcher();   **// here is where the error comes up.** 
    }

myWatcher似乎为null。但是为什么这是空的?这是在启动控制中分配的

FileSystem Watcher对象引用未设置为该对象的实例

button12_Click中,您声明了一个局部变量myWatcher,而不是使用您的类字段(因此,在这一点上,有一个已初始化的局部myWatcher和一个在类级别保持为null的myWatcher):

private void button12_Click(object sender, EventArgs e)
{
    ...
    MyFileWatcher myWatcher = new MyFileWatcher(...);
    ...
}

这就解释了button11_Click中的行myWatcher.RemoveWatcher();抛出异常的原因。

您需要更改button12_Click中的代码以使用类字段,而不是声明一个新的局部变量:

private void button12_Click(object sender, EventArgs e)
{
    ...
    myWatcher = new MyFileWatcher(...);
    ...
}

如果编译此代码,则会在其他地方声明另一个myWatcher

您在button12_Click中设置的myWatcher本地button12_Click:

MyFileWatcher myWatcher = new MyFileWatcher(textBox2, listBox2, destfolder, current, this);

这会使另一个(全局)myWatcher为空,您可以在button11_Click:中访问它

myWatcher.RemoveWatcher();   **// here is where the error comes up.**