Reputation: 418
Here is my snippet
Public async Task DoSomething(List<T> dataList)
{
foreach (T claim in dataList)
{
var claimLineTask = AssembleClaimLines(claim);
tasks.Add(claimLineTask);
}
await Task.WhenAll(tasks);
return dataList;
}
protected async Task AssembleClaimLines(T claim)
{
connector.ConfigSettingsSection = "Claims.Lines";
connector.QueryParameters = new Dictionary<string, string>()
{
{“id”, claim.ID.ToString()},
};
claim.data = await connector.GetData()
return;
}
I have a race condition when calling AssembleClaimLines()
.The values of the connector.QueryParameters
are being overriden by one of the threads. Ultimately, I have lot of duplicate calls.
One easy fix is, I can pass connector
as a parameter for AssembleClaimLines
. But I cannot do that since connector
is passed in from dependency injection. How else can I solve the race condition and still make full use of multi-threading ?
Upvotes: 1
Views: 157
Reputation: 457392
Either your Connector
API needs to be changed to support multiple concurrent calls (e.g., moving ConfigSettingsSection
/QueryParameters
to arguments on GetData
), or you'll need to get multiple Connector
instances (e.g., inject a ConnectorFactory
instead of a Connector
).
Upvotes: 3