Yes there is a possibility:
- Thread 1 runs
Threading.Interlocked.Increment(hitCount)
- Thread 2 runs
Threading.Interlocked.Increment(hitCount)
- Thread 1 runs
Return hitCount
- Thread 2 runs
Return hitCount
In steps 3 and 4, hitCount will be the same value.
But the fix is easy Interlocked.Increment returns the incremented value, so just change your code to:
Private Shared hitCount As Long = 1L
Public Shared Function GetIt() As Long
Return Threading.Interlocked.Increment(hitCount)
End Function
Edit
Or now based on your edit, you have a pretty bit timing hole. Anyway then this is what you want:
Public Shared Function GetIt() As Long
Dim localHitCount As Long = Threading.Interlocked.Increment(hitCount)
Console.Writeline("Something, something....")
Return localHitCount
End Function
Edit
Then do this (which is exactly what Michael suggested below)
Private Shared hitCount As Long = 1L
Public Shared Function GetIt() As Long
Dim localHitCount As Long = Threading.Interlocked.Increment(hitCount)
DoSomethingQuick(localHitCount )
Return localHitCount
End Function