Solved

Taking too long to populate 4,000+ filenames in a ListView object in C#.

Posted on 2008-10-21
5
603 Views
Last Modified: 2013-12-17
I have a LIstView object on my Windows Form that for starters, I want to list around 4,500 + filenames from a remote server. Problem is, its taking around 12 minutes to populate the LV Control.
I've attached my code below. Can anyone show me how to write the code what would allow this app to increase that time using Multithreading/ a delegate? I would at least like to see the filenames be listed and possibly a number count being performed on the files via a Label as well.
Many thanks EE Guru's,
Wally
using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;
 

namespace ParseLogErrors

{

    public partial class Form1 : Form

    {

        private System.Collections.Specialized.StringCollection folderCol;

       

        public Form1()

        {

            InitializeComponent();
 

            // Init ListView and folder collection

            folderCol = new System.Collections.Specialized.StringCollection();

            CreateHeadersAndFillListView();

            PaintListView(@"\\crprdnsrc03\logs$\sd4\regional");

            folderCol.Add(@"\\crprdnsrc03\logs$\sd4\regional");

            

            //this.lvwFilesAndFolders.ItemActivate += System.EventHandler(this.lvwFilesAndFolders_ItemActivate);

        }
 

        private void buttonParseLogs_Click(object sender, EventArgs e)

        {

            string dirName = textBoxSearchPath.Text;

            //DirectoryInfo dir1 = new DirectoryInfo("\\" + dirName);

            

            // Set current working directory & verify it exists

            if (Directory.Exists(dirName))

            try

            {

                Directory.SetCurrentDirectory(dirName);

                MessageBox.Show(dirName);

            }

            catch (UnauthorizedAccessException)

            {

                MessageBox.Show("Not authorized to access " + dirName);

                return;

            }

            catch (FileNotFoundException)

            {

                MessageBox.Show("No such directory: " + dirName);

                return;

            }    

        }
 

        private void CreateHeadersAndFillListView()

        {

            ColumnHeader colHead;
 

            colHead = new ColumnHeader();

            colHead.Width = 500;

            colHead.Text = "Filename";

            this.lvwFilesAndFolders.GridLines = true;

            this.lvwFilesAndFolders.Columns.Add(colHead);
 

            colHead = new ColumnHeader();

            colHead.Width = 100;

            colHead.Text = "Size";

            this.lvwFilesAndFolders.GridLines = true;

            this.lvwFilesAndFolders.Columns.Add(colHead);
 

            colHead = new ColumnHeader();

            colHead.Width = 200;

            colHead.Text = "Last Accessed";

            this.lvwFilesAndFolders.GridLines = true;

            this.lvwFilesAndFolders.Columns.Add(colHead);

        }
 

        private void PaintListView(string root)

        {

            try

            {

                ListViewItem lvi;

                ListViewItem.ListViewSubItem lvsi;
 

                this.labelDisplayLogInfo.Text = root + "    (Double click to display the path name)";

                DirectoryInfo dir = new DirectoryInfo(root);
 

                this.lvwFilesAndFolders.Items.Clear();
 

                DirectoryInfo[] dirs = dir.GetDirectories();

                FileInfo[] files = dir.GetFiles();
 

                this.lvwFilesAndFolders.BeginUpdate();
 

                foreach (System.IO.FileInfo fi in files)

                {

                    lvi = new ListViewItem();

                    lvi.Text = fi.Name;

                    lvi.Tag = fi.FullName;
 

                    lvsi = new ListViewItem.ListViewSubItem();

                    lvsi.Text = formatsizekb(fi.Length);

                    lvi.SubItems.Add(lvsi);
 

                    lvsi = new ListViewItem.ListViewSubItem();

                    lvsi.Text = fi.LastAccessTime.ToString();

                    lvi.SubItems.Add(lvsi);
 

                    this.lvwFilesAndFolders.Items.Add(lvi);

                }
 

                this.lvwFilesAndFolders.EndUpdate();

            }

            catch (System.Exception err)

            {

                MessageBox.Show("Error: " + err.Message);

            }
 

            this.lvwFilesAndFolders.View = View.Details;

        }
 

        private void lvwFilesAndFolders_ItemActive(object sender, System.EventArgs e)

        {

            ListView lvw = (ListView)sender;

            string filename = lvw.SelectedItems[0].Tag.ToString();

            Console.WriteLine(filename);

        }
 

        private void buttonExit_Click(object sender, EventArgs e)

        {

            this.Close();

        }
 

        private string formatsizekb(long lsize)

        {

            const int iKB = 1024;

            const long lMB = 1048576;
 

            if (lsize < iKB)

                return string.Format("{0:#,#} bytes", lsize);

            else if (lsize >= iKB && lsize < lMB) ;

            return string.Format("{0:#,#} KB", lsize / 1024);

        }

    }

}

Open in new window

0
Comment
Question by:wally_davis
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22772012
It might help if you included some of the relevant code like *how* you are adding them to the listview.

Is the time in getting the list of files or in actually adding them to the listbox? I would guess the former. If so it would seem that you would want to get the filenames on a background thread then add them to the listbox as you get them but without more details its really hard to give you direction.

Cheers,

Greg
0
 

Author Comment

by:wally_davis
ID: 22776818
Hi Greg,
If you look under the "private void PaintListView(string root)" subroutine, you'll notice under the foreach loop routine, this is where the files get added to the ListView control, i.e. the "  this.lvwFilesAndFolders.Items.Add(lvi);" Method. It took approximately 12 - 15 minutes to populate 4,500 filenames to the ListView control (named lvwFilesAndFolders).

foreach (System.IO.FileInfo fi in files)
                {
                    lvi = new ListViewItem();
                    lvi.Text = fi.Name;
                    lvi.Tag = fi.FullName;
 
                    lvsi = new ListViewItem.ListViewSubItem();
                    lvsi.Text = formatsizekb(fi.Length);
                    lvi.SubItems.Add(lvsi);
 
                    lvsi = new ListViewItem.ListViewSubItem();
                    lvsi.Text = fi.LastAccessTime.ToString();
                    lvi.SubItems.Add(lvsi);
 
                    this.lvwFilesAndFolders.Items.Add(lvi);
                }
0
 

Author Comment

by:wally_davis
ID: 22777017
For starters, I don't mind using the BackgroundWorker control and then later Delegates. But either one will work.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22778692
Ah sorry my bad I missed the scroll bar ... give me a few minutes to look it over.
0
 

Accepted Solution

by:
wally_davis earned 0 total points
ID: 22791319
Figured out the code I needed to get this to start working. This is not complete but it's a good start.

 protected void bgw_DoWork(object sender, DoWorkEventArgs e)
        {            
            {
                this.Invoke(new MethodInvoker(delegate()
                {
                    progressBar1.PerformStep();

                    /* Lets call the other statements from here
                    to Perform Safe threading execution */
                    string dirName = textBoxSearchPath.Text;
                    folderCol = new System.Collections.Specialized.StringCollection();
                    PaintListView(dirName);
                    folderCol.Add(dirName);
                }));                                
            }                        
        }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 50
Help with Query not working in client's PC 1 35
Javascript and Jquery not firing 9 42
C#.NET and microsoft certification. 3 34
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now