开发者

Adding nodes to TreeView causes Thread-Exception

开发者 https://www.devze.com 2023-02-24 08:49 出处:网络
i have a little problem. I´ve made a class that manages my prefabs (predefined objects for my level-editor).

i have a little problem.

I´ve made a class that manages my prefabs (predefined objects for my level-editor). On loading the prefabs at start, it creates TreeNodes for categories and each prefab and adds it to an TreeView that it knows by the constructor.

The problem know is, that everytime it adds an node to another node, it causes an "InvalidOperationException", because it´s not the right thread. I should invoke the control. I tried it, and it´s the same thread - it´s only called in the "LoadForm"-Event.

Here´s my code of the PrefabManager-class:

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

And here´s the creation and call:

            _flagSystem.AddFlag("PrefabManager", new PrefabManager(tpage_prefabs_tree));
            //...
            _flagSystem.GetFlag<PrefabManager>("PrefabManager").LoadPrefabs();

The error get caused here:

//LoadPrefabs-Method:
categoryNode.Nodes.Add(prefabNode);

What do you think is the problem? I can´t believe it´s an thread problem. How can i solve this problem?

Thanks a lot :)

EDIT Bad that no one knows an answer :( By the way, here is the Stacktrace and some informations about the exception:

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)

{"Der für dieses Steuerelement durchgeführte Vorgang wird vom falschen Thread aufgerufen. Marshallen Sie den richtigen Thread mit Control.Invoke oder Control.BeginInvoke, um den Vorgang auszuführen."}

(Translation) The test performed for this control operation is called from the wrong thread. Marshaling the correct thread or with Control.Invoke Control.BeginInvoke to perform the operation


Err, sorry. Now I got the solution.

I just invoked in the wrong way - so it didn´t work. Here i found something, that showed me how it works: Thread Control.Invoke

Here the sample:

  _tree.Invoke((MethodInvoker) (() => categoryNode.Nodes.Add(prefabNode))); 
0

精彩评论

暂无评论...
验证码 换一张
取 消