1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public delegate void SetListBoxSourceDelegate();
//dans le thread principal
thread2 = new Thread(new ThreadStart(FonctionAppelee));
private void FonctionAppelee()
{
SetListBoxSource();
...
}
private void SetListBoxSource()
{
if (this.InvokeRequired)
{
SetListBoxSourceDelegate d = new SetListBoxSourceDelegate(SetListBoxSource);
this.Invoke(d, new object[] { });
}
else
{
datatable = new DataTable();
DataColumn dc = new DataColumn("string", typeof(string));
datatable.Columns.Add(dc);
DataRow dr = null;
for (int i = 0; i < 3; i++)
{
dr = datatable.NewRow();
dr.BeginEdit();
dr["string"] = "truc" + i.ToString();
dr.EndEdit();
datatable.Rows.Add(dr);
}
listBox.DisplayMember = "string";
listBox.DataSource = datatable;
}
} |