ClsPaginas: Usar ThreadSafeRandom() en vez de Random()

-RAZONAMIENTO:
 Random() no es thread safe, esto puede ocasionar problemas cuando dos hilos intenten acceder a la
 instancia de Random() y que termine generando puros ceros.

 En .NET Core 6.0 se introduce Random.Shared para compartir una instancia de Random() entre hilos distintos.
 https://learn.microsoft.com/en-us/dotnet/api/system.random.shared?view=net-7.0

 Para .NET 4.8, se implementa la clase ThreadSafeRandom() que cumple la misma funcionalidad.
 Basado en https://stackoverflow.com/a/57962385/3529902
This commit is contained in:
MLopez
2023-02-01 19:41:29 -07:00
parent dbdd18e1cc
commit 99facf88ad
2 changed files with 16 additions and 3 deletions

View File

@@ -12,7 +12,6 @@ Imports Microsoft.Reporting.WebForms
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
'NOTA AD TEAM
'COLOR PARA ROW TOTAL EN REPORT VIEWER: #67809F (AZULITO)
@@ -46,8 +45,7 @@ Public Class ClsPaginas
End Property
#Region "Propiedades"
Private Shared ReadOnly Property Rand As New Random()
Private Shared ReadOnly Property Rand As New ThreadSafeRandom()
Private Property SubReportData As Modelos.MSubReportData
Get
Return If(IsNothing(ViewState("SubReportData")), Nothing, DirectCast(ViewState("SubReportData"), Modelos.MSubReportData))

View File

@@ -0,0 +1,15 @@
Imports System.Threading
NotInheritable Class ThreadSafeRandom
Private Shared ReadOnly GlobalRandom As New Random()
Private Shared ReadOnly LocalRandom As New ThreadLocal(Of Random)(Function()
SyncLock GlobalRandom
Return New Random(GlobalRandom.Next())
End SyncLock
End Function)
Function [Next](Optional min As Integer = 0, Optional max As Integer = Integer.MaxValue) As Integer
Return LocalRandom.Value.Next(min, max)
End Function
End Class