2012-06-28 24 views
0

我有以下功能:如何調用AddPath()?

public void AddPath(String full_path) 
    { 
     TreeView tree_view = TheTreeView; 
     String[] split_path; 
     TreeNodeCollection current_nodes; 

     if (tree_view == null) 
      return; 
     if (String.IsNullOrEmpty(full_path)) 
      return; 

     split_path = full_path.Split(tree_view.PathSeparator.ToCharArray()); 
     current_nodes = tree_view.Nodes; 

     for (Int32 i = 0; i < split_path.Length; i++) 
     { 
      TreeNode[] found_nodes = current_nodes.Find(split_path[i], false); 

      if (found_nodes.Length > 0) 
      { 
       current_nodes = found_nodes.First().Nodes; 
      } 
      else 
      { 
       TreeNode node; 

       node = new TreeNode(); 
       node.Name = split_path[i]; // name is the same thing as key 
       node.Text = split_path[i]; 

       current_nodes.Add(node); 
       current_nodes = node.Nodes; 
      } 
     } 
    } 

我需要從一個單獨的線程中調用這個函數。我將如何做到這一點? 我知道如何調用TreeView.Nodes.Add(),但我該如何做到這一點? 0.o

-Swen

+0

這是你需要什麼? http://stackoverflow.com/questions/505523/c-sharp-invoke-a-method-with-type-in​​vokemember-in-a-separate-thread – AdamWhite

回答

1

如果您需要調用一些對象從創建它的一個不同的線程的UI(這是你的情況下)的答案是,你實際上傾斜。更好的答案是你可以通過winform Control.Invoke和WPF Dispatcher.Invoke來調用。只有在winform中,您才能通過使用this method來調查是否實際需要調用調用。一般來說,您應該將異步部分與更新UI的部分分開,以避免太多代碼。