3

약간의 문제가 있습니다.TreeView에 노드를 추가하면 스레드 예외가 발생합니다.

내 프리 패브 (내 레벨 편집기에 미리 정의 된 개체)를 관리하는 클래스를 만들었습니다. 시작시 프리 팹을로드 할 때 카테고리와 각 프리 팹에 대한 TreeNode를 생성하고 생성자가 알고있는 TreeView에 추가합니다.

문제는 다른 노드에 노드를 추가 할 때마다 올바른 스레드가 아니기 때문에 "InvalidOperationException"이 발생한다는 것입니다. 컨트롤을 호출해야합니다. 나는 그것을 시험해 보았다. 그리고 그것은 같은 쓰레드이다 - 그것은 "LoadForm"-Event에서만 호출된다.

Here's 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); 
     } 
    } 

그리고 생성을 here's과 전화 : 오류는 여기에서 발생 얻을

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

:

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

무엇이 문제라고 생각하십니까? 나는 그것이 스레드 문제라고 믿을 수 없다. 어떻게이 문제를 해결할 수 있습니까?

고마워요 :)

편집 아무도 여기에, 그런데 답 :( 을 알고 스택 트레이스 및 예외에 대한 몇 가지 정보입니다 나쁜 :

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) 

{Marshallen Sie den Richtigen Thread with Control.Invoke 또는 Control.BeginInvoke, Vorgang auszuführen. "}

(번역)이 제어 작업에 대해 수행 된 테스트가 잘못된 스레드에서 호출되었습니다. 올바른 스레드를 마샬링하거나 Control.Invoke Control.BeginInvoke를 사용하여 작업을 수행하십시오.

답변

1

오류, 죄송합니다. 이제 해결책을 얻었습니다.

방금 ​​잘못된 방식으로 호출했습니다. 작동하지 않았습니다. 여기 Thread Control.Invoke

샘플 : :이 잊고 계속

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

을, 내 경우에는 내가'경우 (InvokeRequired()) {호출 ((MethodInvoker)를 사용하여 여기에 내가 어떻게 작동하는지 저를 보여 주었다, 무언가를 발견 (() => categoryNode.Nodes.Add (newNode)))); } else {categoryNode.Nodes.Add (newNode); }' –