Surprisingly, this extension did not slow down my PLINQ queries - which I could measure.
In a complex query with many AsParallel () calls, you may need to call SetCulture () after each AsParallel (). I'm not sure if there is one place to add .SetCulture () (or one place for AsParallel, for that matter), so I just added .SetCulture () after each AsParallel () call, and it worked fine.
In addition, you may also consider installing CurrentUICulture. for example, using PLINQ to search for a collection of business objects to search for business objects with broken rules (CSLA structure, Broken Rules assembly) will cause PLINQ streams (stream stream flows) to look for localized (our requirements) string resources to set the string errors (RuleArgs. Description).
I just need to expand the ParallelQueryCultureExtensions extension. This worked for me (I have to use VB.NET, therefore ...):
Public Module PLINQExtensions <Extension()> _ Public Function SetCulture(Of TSource)(ByVal source As ParallelQuery(Of TSource), ByVal culture As CultureInfo, ByVal uiCulture As CultureInfo) As ParallelQuery(Of TSource) SetCulture(culture, uiCulture) Return source.Select(Function(item) SetCulture(culture, uiCulture) Return item End Function ) End Function <Extension()> _ Private Sub SetCulture(ByVal culture As CultureInfo, ByVal uiCulture As CultureInfo) If (Not Thread.CurrentThread.CurrentCulture.Equals(culture)) Then Thread.CurrentThread.CurrentCulture = culture End If If (Not Thread.CurrentThread.CurrentUICulture.Equals(uiCulture)) Then Thread.CurrentThread.CurrentUICulture = uiCulture End If End Sub End Module
user902723
source share