Volatile equivalent in VB.NET

c#, multithreading, vb.net, volatile

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

Problem

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?

Original source

Related problems