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

  • Thread starter Thread starter MarkusR
  • Start date Start date
M

MarkusR

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top