简体   繁体   English

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

[英]FileSystem Watcher Object reference not set to an instance of the object

I'm having a hard time troubleshooting this error. 我很难排除此错误。 It was working before, but maybe I made a mistake somewhere as the code is a bit lenghty. 它以前工作过,但是也许我在某个地方犯了一个错误,因为代码有点冗长。

Here is the code: 这是代码:

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

It's triggered to start by the following control: 它由以下控件触发以启动:

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

It works fine normally. 它正常工作。 but the error comes up when trying to stop by the following control: 但是尝试通过以下控件停止时出现错误:

    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.** 
    }

It seems like myWatcher is null. 似乎myWatcher为null。 But why is this null this was assigned in the start control 但是为什么将此null分配给了启动控件

If this code compiles, you have another myWatcher declared elsewhere. 如果此代码可以编译,则您在其他myWatcher声明了另一个myWatcher

The myWatcher you set in button12_Click is local to button12_Click : myWatcher您在设置button12_Click本地 button12_Click

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

This leaves the other (global) myWatcher null, which you access in button11_Click : myWatcher另一个(全局) myWatcher空,您可以在button11_Click访问button11_Click

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

In button12_Click you are declaring a local variable myWatcher , instead of using your class field (So at that point there is a local myWatcher that has been initialized and a myWatcher at the class level that remains null): button12_Click您声明的是局部变量myWatcher ,而不是使用您的class字段(因此,此时已初始化了一个本地myWatcher ,而在类级别的myWatcher保持为空):

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

That explains why the line myWatcher.RemoveWatcher(); 这就解释了为什么行myWatcher.RemoveWatcher(); in button11_Click throws an exception. button11_Click引发异常。

You need to change the code in button12_Click to use the class field instead of declaring a new local variable: 您需要更改button12_Click的代码以使用类字段,而不是声明新的本地变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM