OK, I am relatively new to C#, so in order to give myself a 'learning' project, I thought I'd update an application I wrote a couple of years ago.
The application uses a ListView control in "Details" mode to display MP3 tag data stored in an Access MDB database file.
The problem I have is that in VB6 the list (containing some 3000+ entries) is populated quite rapidly with only a minimal impact on form drawing that is barely noticeable.
However, in C#, there is a very noticable delay and interpution to the drawing of the form.
Here is the original VB6 code...
// Blank down any items we were displaying and set the Path as the sorted field.
Call frmMain.lstCatView.ListItems.Clear
lstCatView.SortKey = 6
// Check if there are any records in the Database.
If DBHasData > 0 Then
// If we have something to browse, then get a list of every record.
Set Rec = dbCat.OpenRecordset("SELECT * FROM Tracks WHERE 1")
// Step through each record and display it in the ListView control, sorted by Path.
For tmpCntFiles = 0 To DBHasData - 1
tmpDBRows = Rec.GetRows
If tmpDBRows(5, 0) < 1 Or tmpDBRows(5, 0) > 255 Then
frmMain.lstCatView.ListItems.Add (tmpCntFiles + 1), , "??"
Else
frmMain.lstCatView.ListItems.Add (tmpCntFiles + 1), , tmpDBRows(5, 0)
End If
If tmpDBRows(1, 0) = "" Then
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(1) = "<not specified>"
Else
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(1) = tmpDBRows(1, 0)
End If
If tmpDBRows(2, 0) = "" Then
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(2) = "<not specified>"
Else
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(2) = tmpDBRows(2, 0)
End If
If tmpDBRows(3, 0) = "" Then
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(3) = "<not specified>"
Else
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(3) = tmpDBRows(3, 0)
End If
If tmpDBRows(4, 0) < 1900 Or tmpDBRows(5, 0) > Year(Now) Then
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(4) = "????"
Else
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(4) = tmpDBRows(4, 0)
End If
If tmpDBRows(6, 0) > 147 Then tmpDBRows(6, 0) = 148
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(5) = arrGenreName(tmpDBRows(6, 0))
frmMain.lstCatView.ListItems(tmpCntFiles + 1).SubItems(6) = tmpDBRows(7, 0)
Next tmpCntFiles
End If
// Destroy the Record object now that we have finished with it and, lastly, turn on sorting again.
Set Rec = Nothing
lstCatView.Sorted = True
... and here is the C# version (with less field validation being done)...
lstCatView.Items.Clear();
string sql = "SELECT * FROM Tracks";
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandType = CommandType.Text;
conn.Open();
OleDbDataReader reader = cmd.ExecuteReader();
try
{
while (reader.Read())
{
bool EventsCall = false;
object[] DBRecord = new Object[8];
int i = reader.GetValues(DBRecord);
if (EventsCall)
{
// Allow system events to propagate on
// every other iteration.
Application.DoEvents();
EventsCall = !EventsCall;
}
string tmpStrTrk = DBRecord[5].ToString();
int tmpIntTrk = Int32.Parse(tmpStrTrk.Trim());
ListViewItem LVI = new ListViewItem(((tmpIntTrk > 0 && tmpIntTrk < 255) ? tmpIntTrk.ToString() : " - "));
LVI.SubItems.Add(DBRecord[1].ToString());
LVI.SubItems.Add(DBRecord[2].ToString());
LVI.SubItems.Add(DBRecord[3].ToString());
LVI.SubItems.Add(DBRecord[4].ToString());
LVI.SubItems.Add(DBRecord[6].ToString());
LVI.SubItems.Add(DBRecord[7].ToString());
lstCatView.Items.Add(LVI);
}
}
catch(Exception eX)
{
MessageBox.Show("ERROR : " + eX.Message);
}
finally
{
conn.Close();
reader.Close();
cmd.Dispose();
reader.Dispose();
conn.Dispose();
}
... so, why does the C# version appear slower? Is there something I am doing wrong?