2016-10-12 10 views
5

स्टैकफ्लो और सी # नौसिखिया में लोड करने की गति बढ़ाएं!लोडिंग को स्प्रिंग्स की एक सूची को ट्रीव्यू

मेरे पास कुछ कोड है जो एक वृक्षदृश्य नियंत्रण में तारों की एक सूची लोड करता है और यह अच्छी तरह से काम करता है। एकमात्र समस्या गति है। जब सूचियां बड़ी होती हैं तो लोड करने में समय लगता है, जो कि कोई समस्या नहीं है, सिवाय इसके कि यह थोड़ी देर के लिए यूआई को लॉक करता है।

ग: \ drivers \ test1.txt
c: \ drivers \ test2.txt
c:

तो एक उदाहरण है (लेकिन बहुत बड़ा) इस तरह स्ट्रिंग की एक सूची होगा \ drivers \ फ़ोल्डर \ test1.txt
c: \ भाई \ परीक्षक \ text1.zip
c: \ भाई \ एक और \ text2.zip
c: \ डेटा \ company1 \ accounts.rar
c: \ डेटा \ company2 \ accounts.rar

वृक्षदृश्य बैकस्लैश मार्कर का उपयोग करके तारों को विभाजित करता है और उन्हें एक एक्सप्लोरर व्यू में अच्छी तरह से रखता है - यह अच्छा है!

टीवीआरस्टोर उदाहरण में वृक्षदृश्य नियंत्रण है।

foreach (string path in lstKeys) 
     { 

      lastNode = null; 
      subPathAgg = string.Empty; 
      foreach (string subPath in path.Split(new string[] { "\\" }, StringSplitOptions.None)) 
      { 
       foreach (string item in subPath.Split(new string[] { "\\" }, StringSplitOptions.None)) 
       { 
        if (item == "" || item == null) 
        { 
         continue; 
        } 
        subPathAgg += item + "\\"; 

        TreeNode[] n = tvRestore.Nodes.Find(subPathAgg, true); 

        if (n.Length > 0) 
        { 
         lastNode = n[0]; 

         continue; 
        } 
        else 
        { 
         // lastNode = null; 
        } 

        TreeNode[] nodes = tvRestore.Nodes.Find(subPathAgg, true); 
        if (nodes.Length == 0) 
         if (lastNode == null) 
          lastNode = tvRestore.Nodes.Add(subPathAgg, item); 
         else 
          lastNode = lastNode.Nodes.Add(subPathAgg, item); 
        else 
         lastNode = nodes[0]; 
       } 
      } 
     } 

एकमात्र समस्या गति है। मैंने थ्रेड का उपयोग करने की कोशिश की लेकिन कोड अपवाद क्योंकि नियंत्रण एक अलग थ्रेड पर है। मेरा मानना ​​है कि मुझे नोड्स को आमंत्रित करना है। जोड़ें लेकिन मैं यह नहीं समझ सकता कि यह कैसे करें।

आदर्श रूप से कोड प्रारंभ होने पर कोड वृक्षदृश्य को पॉप्युलेट करना शुरू कर देगा, हालांकि मैं नहीं चाहता कि ऐप बड़ी सूचियों के लिए 30-40 सेकंड या उससे अधिक समय तक लॉक अप करे।

इस प्रक्रिया को गति देने का सबसे अच्छा तरीका क्या है?

+2

'treeView1.BeginUpdate(); 'के साथ नोड जोड़ने से पहले चित्र को बंद करने का प्रयास करें और फिर इसे' EndUpdate();' – LarsTech

उत्तर

3

पृष्ठभूमि में इसे चलाने सहित कुछ चीजें आप कर सकते हैं - मुझे इसके लिए कार्य.रुन() का उपयोग करना पसंद है।

private void Form1_Load(object sender, EventArgs e) 
{ 
    // Show the user something 
    treeView1.Nodes.Add("Loading..."); 
    // Run the tree load in the background 
    Task.Run(() => LoadTree()); 
} 

फिर अपने कार्य एक TreeNode अपने सभी नए नोड्स युक्त निर्माण और एक रेंज के रूप में नए नोड्स जोड़ने के लिए, साथ ही का उपयोग कर BeginUpdate ... EndUpdate दृश्य अपडेट रोकने के लिए जब तक सभी नोड्स हैं TreeView आह्वान कर सकते हैं लदा हुआ।

private void LoadTree() 
{ 
    // Get a list of everything under the users' temp folder as an example 
    string[] fileList; 
    DirectoryInfo df = new DirectoryInfo(Path.GetTempPath()); 
    fileList = df.GetFiles("*.*",SearchOption.AllDirectories).Select<FileInfo, string>((f) => f.FullName).ToArray(); 

    // Parse the file list into a TreeNode collection 
    TreeNode node = GetNodes(new TreeNode(), fileList); 

    // Copy the new nodes to an array 
    int nodeCount = node.Nodes.Count; 
    TreeNode[] nodes = new TreeNode[nodeCount]; 
    node.Nodes.CopyTo(nodes, 0); 

    // Invoke the treeview to add the nodes 
    treeView1.Invoke((Action)delegate() 
    { 
     treeView1.BeginUpdate(); // No visual updates until we say 
     treeView1.Nodes.Clear(); // Remove existing nodes 
     treeView1.Nodes.AddRange(nodes); // Add the new nodes 
     treeView1.EndUpdate(); // Allow the treeview to update visually 
    }); 
} 

इस प्रकार मैं ट्री नोड्स की सूची तैयार करूंगा।

private TreeNode GetNodes(TreeNode parent, string[] fileList) 
{ 
    // build a TreeNode collection from the file list 
    foreach (string strPath in fileList) 
    { 
     // Every time we parse a new file path, we start at the top level again 
     TreeNode thisParent = parent; 

     // split the file path into pieces at every backslash 
     foreach (string pathPart in strPath.Split('\\')) 
     { 
      // check if we already have a node for this 
      TreeNode[] tn = thisParent.Nodes.Find(pathPart, false); 

      if (tn.Length == 0) 
      { 
       // no node found, so add one 
       thisParent = thisParent.Nodes.Add(pathPart,pathPart); 
      } 
      else 
      { 
       // we already have this node, so use it as the parent of the next part of the path 
       thisParent = tn[0]; 
      } 
     } 

    } 
    return parent; 
} 

मेरी मशीन पर, यह treeview में इस तरह 56,000 नोड्स लोड करने के लिए लगभग 1.5 सेकंड लेता है, और यूआई नहीं रोक रहा।

संबंधित मुद्दे