Tuesday, January 27, 2015

C: Cross threading issue with async/await

I did try to move the foreach loop into an async method call but I still recieve the Cross-Threading failure on the cbxScriptList.Add(...). What is interesting is that values will be added to this list, but then it will fail despite it was working before.



private async void LoadingAsync()
{
Task task = new Task(() => Loading());
task.Start();
await task;
}
private void Loading()
{
int t = 1;
foreach (string line in scripts)
{
string[] listValues = line.Split(',');
ListViewItem values = new ListViewItem(listValues);
if (t == 1)
{
listView1.Columns.Add("Script Name", 200); // Creates column headings
listView1.Columns.Add("Date and Time", 150);
listView1.Columns.Add("SID", 75);
listView1.Columns.Add("Environment", 75);
listView1.Columns.Add("Client", 75);
t++;
}
else
{
if ((values.Text != "") && (values.Text != "Script Name"))
{
listView1.Items.Add(values);
if (!dictScript.Contains(values.Text))
{
dictScript.Add(values.Text);
cbxScriptList.Items.Add(values.Text);
}
}
}
}
}





SV





No comments:

Post a Comment