Note that I used the keyword "Shared" by doing this your code can get an ID without creating a new object.
The SyncLock Command makes sure only one thread can access it at a time.
The Sleep one millisecond command makes sure if two threads requested data at the same time, they both get different ID's.
Public Class UniqueID
Public Shared ReadOnly Property Generate() As String
Get
Dim UniqueID As String
SyncLock "GENID"
System.Threading.Thread.Sleep(1)
Dim d As String = Now.DayOfYear
Dim y As String = Now.Year
Dim h As String = Now.Hour
Dim m As String = Now.Minute
Dim s As String = Now.Second
Dim ms As String = Now.Millisecond
If d.Length = 1 Then d = "00" & d
If d.Length = 2 Then d = "0" & d
If h.Length = 1 Then h = "0" & h
If m.Length = 1 Then m = "0" & m
If s.Length = 1 Then s = "0" & s
If ms.Length = 1 Then ms = "00" & ms
If ms.Length = 2 Then ms = "0" & ms
UniqueID = y & d & h & m & s & ms
End SyncLock
Return UniqueID
End Get
End Property
End Class
No comments:
Post a Comment