Question

Possible Duplicate:
How do I specify the equivalent of volatile in VB.net?

What is the VB.NET keyword equivalent of C# "volatile"?

If there is no keyword what mechanism is the equivalent?

Was it helpful?

Solution

There is no equivelent to C#'s volatile keywword in VB.NET. Volatile in C# just makes sure the compiler handles things differently when generating the IL, but the VB.NET compiler does not have this option.

You can work around it this way (taken from this blog post):

Function VolatileRead(Of T)(ByRef Address As T) As T
    VolatileRead = Address
    Threading.Thread.MemoryBarrier()
End Function

Sub VolatileWrite(Of T)(ByRef Address As T, ByVal Value As T)
    Threading.Thread.MemoryBarrier()
    Address = Value
End Sub

OTHER TIPS

Use Thread.VolatileRead() and VolatileWrite() methods from the BCL.

http://msdn.microsoft.com/en-us/library/system.threading.thread.volatileread.aspx

Depending on what variable type you use i would suggest using

System.Threading.Thread.VolatileRead()

System.Threading.Thread.VolatileWrite()

Also System.Threading.Interlocked contains some nice stuff...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top