Home > Net >  Async await, how to use them in a windows form?
Async await, how to use them in a windows form?

Time:11-06

string DBconnString = 
  "Provider=Microsoft.Jet.OLEDB.4.0;"   "Data Source="   Para1   @"\Labo_be.mdb;";

var DBConn = new OleDbConnection(DBconnString);
DBConn.Open();

string S_Query = "Select * FROM DISEGNI";
cmd1 = new OleDbCommand(S_Query, DBConn);
DR = cmd1.ExecuteReader();

while (DR.Read())
{
    var sName = DR.GetString(DR.GetOrdinal("PARTNBR"));
    Combo_Dis.Items.Add(sName);
}

How to transform this sub using async await? This sub loads a combo with more than 35000 drawing numbers so the app is very very slow.

CodePudding user response:

If you use WinForms it is Combo_Dis.Items.Add in a loop that is that slow (about 5 seconds on my workstation): when you add a new item, combo box should do some computations (dropdown size), redraw itself etc. You can use BeginUpdate() .. EndUpdate() to do this extra work just once:

using var DBConn = new OleDbConnection(
  $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Path.Combine(Para1, Labo_be.mdb)};"); 

DBConn.Open();

string S_Query = 
  @"SELECT PARTNBR 
      FROM DISEGNI";

using var cmd1 = new OleDbCommand(S_Query, DBConn);

using var dr = cmd1.ExecuteReader();

Combo_Dis.BeginUpdate();

try {
  while (dr.Read()) {
    Combo_Dis.Items.Add(dr[0]);
  }
}
finally {
  Combo_Dis.EndUpdate();
}
  • Related