I'm trying to combine my PLINQ statement like this:
Enumerable.Range(0, _sortedList.Count()).AsParallel().WithDegreeOfParallelism(10)
.Select(i => GetTransactionDetails(_sortedList[i].TransactionID))
.ToList();
With an async method like this:
private async void GetTransactionDetails(string _trID)
{
await Task.Run(() =>
{
});
}
So that I can simply add an await operator in here:
Enumerable.Range(0, _sortedList.Count()).AsParallel().WithDegreeOfParallelism(10)
.Select(i => await GetTransactionDetails(_sortedList[i].TransactionID))
.ToList();
How can I achieve this ?
P.S. This way I could make 5-10 HTTP requests simultaneously while ensuring that the end user doesn't feels any "screen" freezing while doing so...
GetTransactionDetails
is using the async over sync anti-pattern. It shouldn't offload the work to another thread, rather it should just be a synchronous method. If the caller wants to call it withTask.Run
, they can, if they want to do something else, like use PLINQ, then they could do that.Task.Run
fromGetTransactionDetails
, and just have it do the work synchronously, thereby allowing PLINQ to parallelize it.Task.Run
at all, the requests should inherently beTask
returning, and there is no reason at all to be using PLINQ, since you don't have CPU operations you want to perform synchronously, you should simplyawait
the network requests that you have.