0

A FileSystemWatcher is set up to update the TreeView to reflect the files in the directory on disk. It fires as expected and the search through the TreeView returns the correct node, but when I issue the nodeToDelete.Remove() command, I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control '_tvwFiles' accessed from a thread other than the thread it was created on.

I didn't realize I was using more than one thread. Any suggestions on how to remedy this? Or perhaps a different way to gain access to the child node to remove it?

The code is as follows:

using System;
using System.IO;
using System.Windows.Forms;

namespace WaveManager
{
    public partial class FileViewControl : UserControl
    {
        public FileViewControl()
        {
            InitializeComponent();
        }

        private FileSystemWatcher _watcher;
        private const string path = @"c:\temp";

        public void OnWaveOpen(string fileName)
        {
        string filePath = Path.GetDirectoryName(fileName);

        TreeNode folderNode;
        _tvwFiles.ImageIndex = 0;
        _tvwFiles.SelectedImageIndex = 0;
        DirectoryInfo directoryInfo = new DirectoryInfo(filePath);
        //folderNode = new TreeNode(directoryInfo.Name);
        folderNode = new TreeNode(filePath);
        folderNode.Tag = directoryInfo;

        //Getfiles
        TreeNode waveNode;

        foreach (FileInfo wavefile in directoryInfo.GetFiles("*.wav"))
        {
            waveNode = new TreeNode(wavefile.Name, 1, 1);
            waveNode.Name = waveNode.Text;
            waveNode.Tag = wavefile;
            folderNode.Nodes.Add(waveNode);
            }
            _tvwFiles.Nodes.Add(folderNode);

            _tvwFiles.ExpandAll();
        }

        private void OnLoad(object sender, System.EventArgs e)
        {
            if (DesignMode)
                return;
            _tvwFiles.ExpandAll();              
            _watcher = new FileSystemWatcher(path);
            _watcher.EnableRaisingEvents = true;
            _watcher.Deleted += OnFileDeleted;

        }

        private TreeNode FindNodeByTagText(String s, TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                if (node.Tag.ToString() == s)
                    return node;

                TreeNode n = FindNodeByTagText(s, node.Nodes);
                if (n != null)
                    return n;
            }

            return null;
        }

        void OnFileDeleted(object sender, FileSystemEventArgs e)
        {
            string fn = Path.GetFileName(e.FullPath);
            TreeNode nodeToDelete = FindNodeByTagText(fn, _tvwFiles.Nodes);
            nodeToDelete.Remove();
        }
    }
}

Any thoughts would be greatly appreciated. Thank you!

Anik
  • 311
  • 1
  • 5
  • 12
  • possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – Peter Duniho Apr 29 '15 at 04:12

1 Answers1

1

As I remember (I may be mistaken and can't check it right now) FileSystemWatcher uses ThreadPool internally. Maybe that's why you are getting the exception.

Try setting

_watcher.SynchronizingObject = this;
nightcoder
  • 13,149
  • 16
  • 64
  • 72