gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:05:23 25 4
gpt4 key购买 nike

我有个小问题。

我创建了一个类来管理我的预制件(我的关卡编辑器的预定义对象)。在开始加载预制件时,它会为类别和每个预制件创建 TreeNode,并将其添加到构造函数知道的 TreeView 中。

已知的问题是,每次将一个节点添加到另一个节点时,都会导致“InvalidOperationException”,因为它不是正确的线程。我应该调用控件。我试过了,它是同一个线程 - 它只在“LoadForm”-Event 中调用。

这是我的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);
}
}

这是创建和调用:

            _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)

{"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

最佳答案

呃,对不起。现在我得到了解决方案。

我只是以错误的方式调用 - 所以它不起作用。我在这里找到了一些东西,向我展示了它是如何工作的:Thread Control.Invoke

这里是示例:

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

关于c# - 将节点添加到 TreeView 会导致 Thread-Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5640264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com