将节点添加到TreeView会导致Thread-Exception

| 我有一个小问题。 我制作了一个类来管理我的预制件(水平编辑器的预定义对象)。 在开始时加载预制件时,它将为类别和每个预制件创建TreeNodes并将其添加到构造函数知道的TreeView中。 已知的问题是,每次将一个节点添加到另一个节点时,都会导致\“ InvalidOperationException \”,因为它不是正确的线程。我应该调用控件。 我尝试过,它是同一线程-仅在\“ LoadForm \”-Event中调用。 这是我的PrefabManager类代码:
        public PrefabManager(TreeView tree)
    {
        _tree = tree;
        _prefabs = new List<Prefab>();
    }

    public void LoadPrefabs()
    {
        if (!Directory.Exists(_prefabPath))
            Directory.CreateDirectory(_prefabPath);

        _tree.Nodes[\"RootNode\"].Nodes.Clear();

        foreach (string file in Directory.GetFiles(_prefabPath, \"*.pref\", SearchOption.AllDirectories))
        {
            Prefab prefab = Prefab.Load(file);
            if (_prefabs.Count > 0)
                if (_prefabs.Where(pfab => pfab.CreationName == prefab.CreationName).FirstOrDefault() != null) continue;

            TreeNode categoryNode = GetCategoryOrCreate(prefab.Category);
            TreeNode prefabNode = new TreeNode(prefab.CreationName)
                                      {
                                          ImageIndex = 2,
                                          SelectedImageIndex = 2,
                                          Tag = \"Prefab\"
                                      };
            MessageBox.Show(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            categoryNode.Nodes.Add(prefabNode);

            _prefabs.Add(prefab);
        }
    }
这是创建和调用:
            _flagSystem.AddFlag(\"PrefabManager\", new PrefabManager(tpage_prefabs_tree));
            //...
            _flagSystem.GetFlag<PrefabManager>(\"PrefabManager\").LoadPrefabs();
错误在这里引起:
//LoadPrefabs-Method:
categoryNode.Nodes.Add(prefabNode);
您认为出了什么问题?我无法相信这是一个线程问题。 我怎么解决这个问题? 非常感谢 :) 编辑 不好,没人知道答案:( 顺便说一下,这是Stacktrace和有关异常的一些信息:
bei System.Windows.Forms.TreeNode.Realize(Boolean insertFirst)
bei System.Windows.Forms.TreeNodeCollection.AddInternal(TreeNode node, Int32 delta)
bei System.Windows.Forms.TreeNodeCollection.Add(TreeNode node)
bei GooEditor.Prefabs.PrefabManager.GetCategoryOrCreate(String category) in E:\\Sicherung\\Visual Studio 2008\\Projects\\BioHazard\\GooEditor\\Prefabs\\PrefabManager.cs:Zeile 87.
bei GooEditor.Prefabs.PrefabManager.LoadPrefabs() in E:\\Sicherung\\Visual Studio 2008\\Projects\\BioHazard\\GooEditor\\Prefabs\\PrefabManager.cs:Zeile 40.
bei GooEditor.EditorForm.LoadContent() in E:\\Sicherung\\Visual Studio 2008\\Projects\\BioHazard\\GooEditor\\EditorForm.cs:Zeile 202.
bei GooEditor.Editor.LoadContent() in E:\\Sicherung\\Visual Studio 2008\\Projects\\BioHazard\\GooEditor\\Editor.cs:Zeile 117.
bei Microsoft.Xna.Framework.Game.Initialize()
bei GooEngine.Core.Application.Initialize() in E:\\Sicherung\\Visual Studio 2008\\Projects\\BioHazard\\GooEngine\\Core\\Application.cs:Zeile 85.
bei Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
bei Microsoft.Xna.Framework.Game.Run()
bei XNAViewer.XNAViewer.StartGameLoop(Object game)
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart(Object obj)
  {\“ Derfürdies SteuerelementdurchgeführteVorgang wird vom falschen线程aufgerufen。MarshallenSie den richtigen线程mit Control。调用oderControl。BeginInvoke,um den Vorgangauszuführen。”“      (翻译)为此控制操作执行的测试是从错误的线程调用的。编组正确的线程或使用Control.Invoke Control.BeginInvoke来执行操作     
已邀请:
不好意思。现在我得到了解决方案。 我只是以错误的方式调用-所以它没有用。在这里,我发现了一些东西,向我展示了它是如何工作的:Thread Control.Invoke 这里是示例:
  _tree.Invoke((MethodInvoker) (() => categoryNode.Nodes.Add(prefabNode))); 
    

要回复问题请先登录注册