-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Parallel.ForEachAsync #46943
Add Parallel.ForEachAsync #46943
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
...ibraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs
Outdated
Show resolved
Hide resolved
...ibraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs
Show resolved
Hide resolved
...ibraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.ForEachAsync.cs
Show resolved
Hide resolved
Also clean up a few things in the src, adding a few more asserts and moving nullable suppressions closer to the point where they're relevant.
da07fdc
to
2d9286a
Compare
Hi all. I played a bit with the Something strange also happens with this specific scheduler ( Thanks for the good work on this very much needed API! |
Can you please open an issue with a repro? Thanks. |
Oh, CurrentThreadTaskScheduler. If there's a sync ctx on the current thread, the awaits in the implementation will see it. That's expected, and there's little that can or should be done about that. That sample CurrentThreadTaskScheduler does not make sense to use with anything on Parallel, as "run synchronously on the current thread" and "parallel" are opposites ;-) |
What I find compelling with the idea of combining |
No, the About posting a repro, I am not familiar with how GitHub works, but I could post something on dotnetfiddle.net if you want. |
What are you trying to achieve? I'm not understanding why you're using Parallel.ForEachAsync. It sounds like you just want a regular (And fwiw, CurrentThreadTaskScheduler is definitely not what you want, regardless of sync ctx. All it does is run a queued work item synchronously instead of queueing it. It doesn't run a continuation somehow back on the original thread.) |
This a hypothetical usage example of the private async void Button_Click(object sender, EventArgs e)
{
List<string> urls = GetUrls();
await Parallel.ForEachAsync(urls, new ParallelOptions()
{
TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext()
}, async (url, token) =>
{
Debug.WriteLine(TaskScheduler.Current);
var document = await _httpClient.GetStringAsync(url, token);
ListBox1.Items.Add($"{url}: {document.Length} chars");
});
} It seems to work correctly with both a |
Ah, you're trying to overlap the awaits but run all work on the UI thread. TaskScheduler and SychronizationContext are both effectively schedulers, so when there's work to be queued, really only one can be used. If what you want is for all scheduling to be back to the original SynchronizationContext, try using TaskScheduler.FromCurrentSynchronizationContext. |
Yes, that's the idea. From my experience in StackOverflow the last couple of years, most folks who are searching for an async-friendly version of Thanks for the insight about the "conflict" between the |
Fixes #1946
cc: @kouvel, @tarekgh, @tomesendam