Export (0) Print
Expand All

Interlocked.Decrement Method (Int32)

Decrements a specified variable and stores the result, as an atomic operation.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function Decrement ( _
	ByRef location As Integer _
) As Integer


Type: System.Int32

The variable whose value is to be decremented.

Return Value

Type: System.Int32
The decremented value.


The address of location is a null pointer.

This method handles an overflow condition by wrapping: If location = Int32.MinValue, location - 1 = Int32.MaxValue. No exception is thrown.

The following code example shows a thread-safe way to increment and decrement an integer value. SafeInstanceCount will always be zero. However, UnsafeInstanceCount will not necessarily be zero because a race condition occurs between incrementing and decrementing the count. This effect is especially marked on a multiprocessor computer.


The C++ example does not follow the IDisposable.Dispose pattern. (See Destructors and Finalizers in Visual C++.) The finalizer is used to decrement the counters, so that garbage collection causes decrementing.

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class Test

    <MTAThread> _
    Shared Sub Main()
        Dim thread1 As New Thread(AddressOf ThreadMethod)
        Dim thread2 As New Thread(AddressOf ThreadMethod)

        ' Have the garbage collector run the finalizer for each 
        ' instance of CountClass and wait for it to finish.

        Console.WriteLine("UnsafeInstanceCount: {0}" & _
            vbCrLf & "SafeCountInstances: {1}", _
            CountClass.UnsafeInstanceCount.ToString(), _
   End Sub 

   Shared Sub ThreadMethod()
        Dim cClass As CountClass 

        ' Create 100,000 instances of CountClass. 
        For i As Integer = 1 To 100000
            cClass = New CountClass()
        Next i
   End Sub 

End Class 

Public Class CountClass

    Shared unsafeCount As Integer = 0
    Shared   safeCount As Integer = 0

    Shared ReadOnly Property UnsafeInstanceCount As Integer 
            Return unsafeCount
        End Get 
    End Property 

    Shared ReadOnly Property SafeInstanceCount As Integer 
            Return safeCount
        End Get 
    End Property 

    Sub New()
        unsafeCount += 1
    End Sub 

    Protected Overrides Sub Finalize()
        unsafeCount -= 1
    End Sub 

End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft