Performance issue
-
Hi, I am facing with a performance issue in one of my windowsapplication. I need to create a dialog window for excluding subfolders and files in a particular folder (passed as input). The Exclude dialog must allow the user to select folders and files which he wants to exclude from counting. Currently I am using a tree view and list view (both with checkboxes) for this purpose.
Implementation: 1. I bind the tree view with all subfolders on the load time. 2. The list view shows the files and folders of each selected node in the tree. 3. When a node or list item checked or unchecked, all its sub-items has to get checked or unchecked. Issue faced: 1. In case of a large folder (with large no. of Subfolders and files), it takes long time for loading the tree (to bind all these sub-folders and files to tree) 2. If I go for an implementation of binding only two levels to the tree node each time (on load and on node click), issue occurs when I uncheck a node or list item. Only the sub items up to 2 level gets unchecked. 3. Check- Uncheck for large subfolders too takes long time.
Can anyone help me out with a solution for this? Even a design change or change in the current implementation can be suggested. Thanks in Advance Praji -
Hi, I am facing with a performance issue in one of my windowsapplication. I need to create a dialog window for excluding subfolders and files in a particular folder (passed as input). The Exclude dialog must allow the user to select folders and files which he wants to exclude from counting. Currently I am using a tree view and list view (both with checkboxes) for this purpose.
Implementation: 1. I bind the tree view with all subfolders on the load time. 2. The list view shows the files and folders of each selected node in the tree. 3. When a node or list item checked or unchecked, all its sub-items has to get checked or unchecked. Issue faced: 1. In case of a large folder (with large no. of Subfolders and files), it takes long time for loading the tree (to bind all these sub-folders and files to tree) 2. If I go for an implementation of binding only two levels to the tree node each time (on load and on node click), issue occurs when I uncheck a node or list item. Only the sub items up to 2 level gets unchecked. 3. Check- Uncheck for large subfolders too takes long time.
Can anyone help me out with a solution for this? Even a design change or change in the current implementation can be suggested. Thanks in Advance PrajiKP_Prajilal wrote:
In case of a large folder (with large no. of Subfolders and files), it takes long time for loading the tree (to bind all these sub-folders and files to tree)
You could fill the node when it's opened. Just fill the root and perhaps some child levels in the beginning
KP_Prajilal wrote:
If I go for an implementation of binding only two levels to the tree node each time (on load and on node click), issue occurs when I uncheck a node or list item. Only the sub items up to 2 level gets unchecked
You can either fill the child nodes for the selected node upon selecting but you can also change the logic so that if the child nodes aren't filled, the outer node selection automatically implies to all subfolders even if they are not in the tree. So when working on user's choices (what ever you do with them) you don't expect to find every folder in the tree.
The need to optimize rises from a bad design.My articles[^]
-
KP_Prajilal wrote:
In case of a large folder (with large no. of Subfolders and files), it takes long time for loading the tree (to bind all these sub-folders and files to tree)
You could fill the node when it's opened. Just fill the root and perhaps some child levels in the beginning
KP_Prajilal wrote:
If I go for an implementation of binding only two levels to the tree node each time (on load and on node click), issue occurs when I uncheck a node or list item. Only the sub items up to 2 level gets unchecked
You can either fill the child nodes for the selected node upon selecting but you can also change the logic so that if the child nodes aren't filled, the outer node selection automatically implies to all subfolders even if they are not in the tree. So when working on user's choices (what ever you do with them) you don't expect to find every folder in the tree.
The need to optimize rises from a bad design.My articles[^]
-
I want to bind all the sub folders and file of a root folder when selecting it. Could u please give me a sample code?
The idea was that you don't take all the folders to the tree, only the nodes are selected. You can use BeforeExpand[^] event and in that event you fill the folders right under that node (if not already filled). When you perform the operation (whatever it is) you check if the node is present and based on the selection you do the operation. You can use recursion in the operation just check if node is selected and if it is, recurse child nodes if there are any. if there aren't recurse, the folders under that folder and do the operation if the node was selected.
The need to optimize rises from a bad design.My articles[^]
-
The idea was that you don't take all the folders to the tree, only the nodes are selected. You can use BeforeExpand[^] event and in that event you fill the folders right under that node (if not already filled). When you perform the operation (whatever it is) you check if the node is present and based on the selection you do the operation. You can use recursion in the operation just check if node is selected and if it is, recurse child nodes if there are any. if there aren't recurse, the folders under that folder and do the operation if the node was selected.
The need to optimize rises from a bad design.My articles[^]
-
I tried it and got better performance while binding the folders. But when Check/Uncheck a root folder performance is too low(for Check/Uncheck the sub folders). How can i solve this? Thanks in Advance Praji
DJ245 wrote:
But when Check/Uncheck a root folder performance is too low(for Check/Uncheck the sub folders). How can i solve this?
Without seeing the code you've made, it's quite impossible to say why it's slow. When the root node is checked, do you for example recursively traverse all of the subnodes and check them also? That would explain why it's slow. Also if that's the case, it depends how you have done it. Can you post the code that is used when checking a node?
The need to optimize rises from a bad design.My articles[^]
-
DJ245 wrote:
But when Check/Uncheck a root folder performance is too low(for Check/Uncheck the sub folders). How can i solve this?
Without seeing the code you've made, it's quite impossible to say why it's slow. When the root node is checked, do you for example recursively traverse all of the subnodes and check them also? That would explain why it's slow. Also if that's the case, it depends how you have done it. Can you post the code that is used when checking a node?
The need to optimize rises from a bad design.My articles[^]
Code used :
#region CheckUncheckChildNodes /// /// This method is used to check/uncheck the subsequent child nodes on checking a node. /// private void CheckUncheckChildNodes(TreeNode node) { childCheck = true; //if the selected node is checked if (node.Checked == true) { //check if the selected node already present in removedFileList, //if not add the node to initalFileList (which has the list of selected files and folders) bool isExists = false; for (int index = 0; index < removedFileList.Count; index++) { string name = removedFileList\[index\].ToString().Replace("\\\\\\\\", "\\\\"); if (name == node.FullPath) { isExists = true; break; } } if (isExists) removedFileList.Remove(node.FullPath); //check all the filtered files in the selected node CheckUncheckFilteredFiles(node, node.Checked); //check all the sub-nodes of the selected node foreach (TreeNode childNode in node.Nodes) { childNode.Checked = true; CheckUncheckChildNodes(childNode); } } //if the selected node is not checked else { //check if the selected node already present in initalFileList, //if present, remove the node from initalFileList (which has the list of selected files and folders) bool isExists = false; for (int index = 0; index < removedFileList.Count; index++) { string name = removedFileList\[index\].ToString().Replace("\\\\\\\\", "\\\\"); if (name == node.FullPath) { isExists = true; break; } } if (!isExists) removedFileList.Add(node.FullPath); //uncheck all the filtered files in the selected node CheckUncheckFilteredFiles(node, node.Checked); //uncheck all the sub-nodes of the selected node forea
-
Code used :
#region CheckUncheckChildNodes /// /// This method is used to check/uncheck the subsequent child nodes on checking a node. /// private void CheckUncheckChildNodes(TreeNode node) { childCheck = true; //if the selected node is checked if (node.Checked == true) { //check if the selected node already present in removedFileList, //if not add the node to initalFileList (which has the list of selected files and folders) bool isExists = false; for (int index = 0; index < removedFileList.Count; index++) { string name = removedFileList\[index\].ToString().Replace("\\\\\\\\", "\\\\"); if (name == node.FullPath) { isExists = true; break; } } if (isExists) removedFileList.Remove(node.FullPath); //check all the filtered files in the selected node CheckUncheckFilteredFiles(node, node.Checked); //check all the sub-nodes of the selected node foreach (TreeNode childNode in node.Nodes) { childNode.Checked = true; CheckUncheckChildNodes(childNode); } } //if the selected node is not checked else { //check if the selected node already present in initalFileList, //if present, remove the node from initalFileList (which has the list of selected files and folders) bool isExists = false; for (int index = 0; index < removedFileList.Count; index++) { string name = removedFileList\[index\].ToString().Replace("\\\\\\\\", "\\\\"); if (name == node.FullPath) { isExists = true; break; } } if (!isExists) removedFileList.Add(node.FullPath); //uncheck all the filtered files in the selected node CheckUncheckFilteredFiles(node, node.Checked); //uncheck all the sub-nodes of the selected node forea
Lets start with the
CheckUncheckChildNodes
method. You have an arrayremovedFileList
. Instead of using an array and always looping through it, use for example HashTable. Something like this when you add new files to the list (or whatever code you use when you add to the array):private System.Collections.HashTable removedFileList = new System.Collections.HashTable();
...
// when adding to HashTable:
removedFileList.Add(node.FullPath, "");Now when you have the CheckUncheckChildNodes method, you could modify your code to this:
...
//check if the selected node already present in removedFileList,
//if not add the node to initalFileList (which has the list of selected files and folders)
if (removedFileList.ContainsKey(node.FullPath))
removedFileList.Remove(node.FullPath);
//check all the filtered files in the selected node
CheckUncheckFilteredFiles(node, node.Checked);
...The same applies to the else section if the selected node is not checked. That would speed up the execution and use less code. The same idea can be used in
CheckUncheckFilteredFiles
The need to optimize rises from a bad design.My articles[^]
-
Lets start with the
CheckUncheckChildNodes
method. You have an arrayremovedFileList
. Instead of using an array and always looping through it, use for example HashTable. Something like this when you add new files to the list (or whatever code you use when you add to the array):private System.Collections.HashTable removedFileList = new System.Collections.HashTable();
...
// when adding to HashTable:
removedFileList.Add(node.FullPath, "");Now when you have the CheckUncheckChildNodes method, you could modify your code to this:
...
//check if the selected node already present in removedFileList,
//if not add the node to initalFileList (which has the list of selected files and folders)
if (removedFileList.ContainsKey(node.FullPath))
removedFileList.Remove(node.FullPath);
//check all the filtered files in the selected node
CheckUncheckFilteredFiles(node, node.Checked);
...The same applies to the else section if the selected node is not checked. That would speed up the execution and use less code. The same idea can be used in
CheckUncheckFilteredFiles
The need to optimize rises from a bad design.My articles[^]
-
One question is that do you still have all the files and directories initially in the tree view. I'm asking this because both listview and treeview can handle few thousand rows. After that the performance degrades radically. The next step is that is the CheckUncheckChildNodes method slow or the CheckUncheckFilteredFiles method? I would guess that it's CheckUncheckFilteredFiles. You can try this by commenting out the call to CheckUncheckFilteredFiles from CheckUncheckFilteredFiles. I figured out that you're listing both files and folders in the same treeview but you exclude some files based on their extension or is it vice versa, if the user has selected a parser, do you show only files with certain extension? If you show only certain extensions, use serchpattern on
GetFiles
Then you also show an icon based on the file. However did I understand correctly that you have a separate icon for every file??subNode.ImageIndex = sysIcons.GetIconIndex(fil.FullName)
Also I don't seem to find that you are clearing subnodes anywhere so if the directory is read twice, won't you have duplicate rows on the tree? One way to enhance the performance is to use a tree for folders only and a list view for files, same as Windows Explorer. By separating them you wouldn't have so many items in the tree. Also, what's the whole idea. You have a form where you have a combobox? for parser selection. After selecting the parser, user can select what files are parsed? All the files that are selected are parsed and if a node is parsed then every file in that node and it's subnodes are parsed? Was that even close?
The need to optimize rises from a bad design.My articles[^]
-
One question is that do you still have all the files and directories initially in the tree view. I'm asking this because both listview and treeview can handle few thousand rows. After that the performance degrades radically. The next step is that is the CheckUncheckChildNodes method slow or the CheckUncheckFilteredFiles method? I would guess that it's CheckUncheckFilteredFiles. You can try this by commenting out the call to CheckUncheckFilteredFiles from CheckUncheckFilteredFiles. I figured out that you're listing both files and folders in the same treeview but you exclude some files based on their extension or is it vice versa, if the user has selected a parser, do you show only files with certain extension? If you show only certain extensions, use serchpattern on
GetFiles
Then you also show an icon based on the file. However did I understand correctly that you have a separate icon for every file??subNode.ImageIndex = sysIcons.GetIconIndex(fil.FullName)
Also I don't seem to find that you are clearing subnodes anywhere so if the directory is read twice, won't you have duplicate rows on the tree? One way to enhance the performance is to use a tree for folders only and a list view for files, same as Windows Explorer. By separating them you wouldn't have so many items in the tree. Also, what's the whole idea. You have a form where you have a combobox? for parser selection. After selecting the parser, user can select what files are parsed? All the files that are selected are parsed and if a node is parsed then every file in that node and it's subnodes are parsed? Was that even close?
The need to optimize rises from a bad design.My articles[^]
I used this to count the LOC of a whole project. For example my project folder contains around 250 folders and 25000 files. A tree view is used to list out the folders with check boxes and a list view to display all the files under selected folder. Both the two functions are slow. Main form contains a combo box for parser selection and another combo box for selecting folder.