.net自定义形式即使处理程序从不同的线程调用和崩溃VS [英] .net Custom Form Even Handlers getting called from different thread and crashing VS

查看:276
本文介绍了.net自定义形式即使处理程序从不同的线程调用和崩溃VS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2012做一个.dll,其中包含一个扩展Windows窗体TreeView窗体的类。我的自定义TreeView叫FolderTreeView。在它内部,我添加一些我需要的私有字段,主要是一个包含DriveInfo和相关FileSystemWatcher的元组列表。

I am using Visual Studio 2012 to make a .dll that contains a class that extends the Windows Forms TreeView form. My custom TreeView is called FolderTreeView. Inside of it I add some private fields that I need, mainly being a List of Tuple that contains a DriveInfo and an associated FileSystemWatcher.



foreach (var drive in DriveInfo.GetDrives())
            {
                if (drive.IsReady == true)
                {
                    FileSystemWatcher watcher = new FileSystemWatcher(drive.RootDirectory.FullName);
                    //_drives is List of Tuples
                    _drives.Add(new Tuple<DriveInfo, FileSystemWatcher>(drive, watcher));
                    watcher.NotifyFilter = NotifyFilters.DirectoryName;
                    watcher.IncludeSubdirectories = true;
                    watcher.Created += new FileSystemEventHandler(FileSystemWatcher_OnCreated);
                    watcher.Changed += new FileSystemEventHandler(FileSystemWatcher_OnChange);
                    watcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_OnDelete);
                    watcher.Renamed += new RenamedEventHandler(FileSystemWatcher_OnRename);
                    watcher.EnableRaisingEvents = true;

                    Nodes.Add(drive.RootDirectory.Name);

                }
            }

此代码导致两个问题,我怀疑与偶数处理程序。第一个问题是FileSystemWatcher的事件正在从不同的线程被调用,所以它抛出异常,因为其他线程不应该被允许访问Windows窗体。

This code causes two problems, both I suspect with the even handlers. The first problem is that the events for the FileSystemWatcher are getting called from a different thread, so it's throwing exceptions since other threads shouldn't be allowed to access Windows Forms.

第二个问题是,如果我设置的FileSystemWatcher的重命名事件处理程序的代码没有注释掉,我在Windows资源管理器中更改文件夹名称,Visual Studio崩溃,我不知道为什么。我似乎很可能是由重命名的事件处理程序引起的。

The second issue is that if the code where I set the Renamed event handler for the FileSystemWatcher is NOT commented out and I change a folder name in Windows Explorer, Visual Studios crashes, and I have no idea why. I seems like it's very likely to be caused by the Renamed event handler.

我想帮助尝试修复线程问题,因为也许这将修复崩溃,除非有另一个原因可能发生。此外,它会更好地处理所有的文件系统东西和节点建立在不同的类,只是从所述类中获得一个节点,并将其提供给一个常规的TreeView?

I would like help trying to fix the thread problem first, because maybe that will fix the crashes unless there is another reason this may be happening. Also, would it be better to handle all the Filesystem stuff and node building in a different class, and just get a node from said class and give it to a regular TreeView?

编辑:我相信它与线程有关。当它崩溃时,我可以在Visual Studio的另一个实例中调试,我得到空引用异常超过我有断点。这让我有理由相信这些事件是在另一个线程中触发的,所以我的断点不会被它认为应该在其上的线程触发。

I do believe it has to do with threads. When it crashes, I can debug in another instance of Visual Studios, and I am getting null reference exceptions past where I have breakpoints. This gives me reason to believe that the events are being triggered in another thread, so my breakpoints are not getting hit by the thread it thinks it's supposed to be on?

推荐答案

在你的事件处理程序中,如果你想操纵Windows窗体控件,你需要检查 Form / Control.InvokeRequired http://msdn.microsoft.com/ en-us / library / system.windows.forms.control.invokerequired.aspx

In your event handlers, if you want to manipulate the Windows Forms controls, you will need to check the Form/Control.InvokeRequired, see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx

如果是真的,那么你将从另一个线程比UI线程。在这种情况下,使用 Form / Control.Invoke 将事件排在UI线程上,请参阅: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx

If it's true, then you're being called from a different thread than the UI thread. In that case use Form/Control.Invoke to have the event queued on the UI thread, see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx

另请参阅: http://msdn.microsoft.com/en-us/library/ms171728(v = vs.85).aspx

这篇关于.net自定义形式即使处理程序从不同的线程调用和崩溃VS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆