Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Populating an in-memory table is really slow (VS2005,c#)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MarkusR - 17 Aug 2006 22:10 GMT
Good day,

I am using c# from VS2005.

I am loading a datatable with the filenames and such from a given
directory using the following code:

arrFileList = GetAllFiles("c:\\sample", true);

dtJobs.BeginLoadData();
try
{
 foreach (string sFile in arrFileList)
 {
   NewDataRow = dtJobs.NewRow();

   sFileName = Path.GetFileName(sFile);
   sFilePath = Path.GetDirectoryName(sFile);
   NewDataRow.ItemArray = new object[] {sFileName, sFilePath};
   dtJobs.Rows.Add(NewDataRow);
 }
}
finally
{
 dtJobs.EndLoadData();
}

I am  using a table because I plan to use the DexExpress XtraGrid.

But for testing there are only 20 or some files in the directory and
yet this method is rather slow. Is there a better way to populate the
table (there is a timer and the data will be refreshed on a regular
basis).

The loading of the table is the slow part. The directory access and
everything else is very quick.

Thanks for any ideas.

-Markus_R
Jon Skeet [C# MVP] - 17 Aug 2006 22:21 GMT
<snip>

> The loading of the table is the slow part. The directory access and
> everything else is very quick.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

MarkusR - 17 Aug 2006 23:46 GMT
Jon wrote:

> Could you post a short but complete program which demonstrates the
> problem?

Hey Jon,

Good exercise for me. I quickly found the problem that I overlooked. I
lookup the filename in a database and that was the slowdown. <doh>

I will post my database access question instead.

I am posting my example in case it helps any one populate an in-memory
table.

namespace SampleListDir
{
   public partial class Form1 : Form
   {
       private DataTable data;

       public Form1()
       {
           InitializeComponent();

           string[] arrFileList;
           string sFileName;
           int i = 1;

           data = new DataTable("dtJobs");
           data.BeginInit();
           AddColumn(data, "ID",
                     System.Type.GetType("System.Int32"), true);
           AddColumn(data, "Filename",
                     System.Type.GetType("System.String"),false);
           data.EndInit();

           data.Clear();

           this.Cursor = Cursors.WaitCursor;
           data.BeginLoadData();
           try
           {
               arrFileList =
                 Directory.GetFiles("C:\\WINDOWS\\system", "*.*", 0);
               foreach (string sFile in arrFileList)
               {
                   sFileName = Path.GetFileName(sFile);
                   data.Rows.Add(new object[] { i, sFileName});
                   i++;
               }
           }
           finally
           {
               data.EndLoadData();
               this.Cursor = Cursors.Default;
           }
           MessageBox.Show("done");
       }

       private void AddColumn(DataTable data, string name,
                              System.Type type, bool ro)
       {
           DataColumn col;
           col = new DataColumn(name, type);
           col.Caption = name;
           col.ReadOnly = ro;
           data.Columns.Add(col);
       }
   }
}

Thanks for you help. :)

-Markus_R

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.