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:
@@ -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))
|
||||
|
||||
15
App_Code/ThreadSafeRandom.vb
Normal file
15
App_Code/ThreadSafeRandom.vb
Normal 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
|
||||
Reference in New Issue
Block a user