I am trying to improve the speed of execution of a program. I use the task to make it run concurrently. The following is my original code:
StringBuilder expression = new StringBuilder(1024);
int count = table.Rows.Count;
for (int i = 0; i < count; i++)
{
string result = GetResult(companyID);
expression.Append(result);
}
And I change to something like this:
StringBuilder expression = new StringBuilder(1024);
int count = table.Rows.Count;
for (int i = 0; i < count; i++)
{
try
{
Task task1 = Task.Factory.StartNew(() =>
{
string result = GetResult(companyID);
expression.Append(result);
});
}
catch (Exception ex) {
}
}
However, the result is not the same. How can I make the result same with the use of task?