From ed012cb59b2140f8e45839136669445387d31cea Mon Sep 17 00:00:00 2001 From: MLopez Date: Wed, 1 Feb 2023 20:23:46 -0700 Subject: [PATCH] =?UTF-8?q?ClsPaginas:=20ThreadSafeRandom=20es=20est=C3=A1?= =?UTF-8?q?tico=20-En=20la=20conversi=C3=B3n=20de=20C#=20a=20VB.NET=20se?= =?UTF-8?q?=20me=20pas=C3=B3=20que=20la=20clase=20es=20completamente=20est?= =?UTF-8?q?=C3=A1tica=20=20y=20es=20innecesario=20instanciarla.=20Basta=20?= =?UTF-8?q?con=20llamar=20a=20ThreadSafeRandom.Next()=20=20Lo=20que=20es?= =?UTF-8?q?=20mejor,=20as=C3=AD=20queda=20mas=20expl=C3=ADcito.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App_Code/ClsPaginas.vb | 3 +-- App_Code/ThreadSafeRandom.vb | 7 ++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/App_Code/ClsPaginas.vb b/App_Code/ClsPaginas.vb index 7aa2895e1..75dad3cae 100644 --- a/App_Code/ClsPaginas.vb +++ b/App_Code/ClsPaginas.vb @@ -45,7 +45,6 @@ Public Class ClsPaginas End Property #Region "Propiedades" - 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)) @@ -6186,7 +6185,7 @@ Public Class ClsPaginas Public Shared Sub RegistraScript(Script As String, Optional valuetick As String = "") Dim page As Page = TryCast(HttpContext.Current.CurrentHandler, Page) Script = $"{Script.TrimEnd(";"c)};" 'sino trae ";" al final, se lo agregamos - ScriptManager.RegisterStartupScript(page, page.GetType(), $"{DateTime.Now.Ticks + 1}{Rand.Next()}{valuetick}", Script, True) + ScriptManager.RegisterStartupScript(page, page.GetType(), $"{DateTime.Now.Ticks + 1}{ThreadSafeRandom.Next()}{valuetick}", Script, True) End Sub Public Sub TabEnabled(Tab As System.Web.UI.HtmlControls.HtmlAnchor, Valor As Boolean) If Valor Then diff --git a/App_Code/ThreadSafeRandom.vb b/App_Code/ThreadSafeRandom.vb index 825913ccb..fc20be441 100644 --- a/App_Code/ThreadSafeRandom.vb +++ b/App_Code/ThreadSafeRandom.vb @@ -1,15 +1,16 @@ Imports System.Threading NotInheritable Class ThreadSafeRandom + Private Sub New() + 'Impide que se instancie la clase (equivalente a static class en C#) + End Sub 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 + Public Shared 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 \ No newline at end of file