This documentation is archived and is not being maintained.

Interlocked.Increment Method (Int32)

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

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

'Declaration
Public Shared Function Increment ( _
	ByRef location As Integer _
) As Integer
'Usage
Dim location As Integer
Dim returnValue As Integer

returnValue = Interlocked.Increment(location)
public static int Increment (
	/** @ref */ int location
)
Not applicable.

Parameters

location

The variable whose value is to be incremented.

Return Value

The incremented value.

Exception typeCondition

NullReferenceException

The address of location is a null pointer.

This method handles an overflow condition by wrapping: if location = Int32.MaxValue, location + 1 = Int32.MinValue. 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.

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)
        thread1.Start()
        thread2.Start()
        thread1.Join()
        thread2.Join()

        ' Have the garbage collector run the finalizer for each
        ' instance of CountClass and wait for it to finish.
        GC.Collect()
        GC.WaitForPendingFinalizers()

        Console.WriteLine("UnsafeInstanceCount: {0}" & _
            vbCrLf & "SafeCountInstances: {1}", _
            CountClass.UnsafeInstanceCount.ToString(), _
            CountClass.SafeInstanceCount.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()
            cClass = Nothing
            i += 1
        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
        Get
            Return unsafeCount
        End Get
    End Property

    Shared ReadOnly Property SafeInstanceCount As Integer
        Get
            Return safeCount
        End Get
    End Property
   
    Sub New()
        unsafeCount += 1
        Interlocked.Increment(safeCount)
    End Sub

    Protected Overrides Sub Finalize()
        unsafeCount -= 1
        Interlocked.Decrement(safeCount)
        MyBase.Finalize()
    End Sub

End Class

import System.*;
import System.Threading.*;
import System.Threading.Thread;

class Test
{
    public static void main(String[] args)
    {
        Thread thread1 = new Thread(new ThreadStart(ThreadMethod));
        Thread thread2 = new Thread(new ThreadStart(ThreadMethod));

        thread1.Start();
        thread2.Start();
        thread1.Join();
        thread2.Join();

        // Have the garbage collector run the finalizer for each
        // instance of CountClass and wait for it to finish.
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine("UnsafeInstanceCount: {0}" + 
            "\nSafeCountInstances: {1}", 
            String.valueOf(CountClass.get_UnsafeInstanceCount()), 
            String.valueOf(CountClass.get_SafeInstanceCount()));
    } //main

    static void ThreadMethod()
    {
        CountClass cClass;

        // Create 100,000 instances of CountClass.
        for (int i = 0; i < 100000; i++) {
            cClass = new CountClass();
        }
    } //ThreadMethod
} //Test

class CountClass
{
    private static int unsafeInstanceCount = 0;
    private static int safeInstanceCount = 0;

    /** @property
     */
    public static int get_UnsafeInstanceCount()
    {
        return unsafeInstanceCount;
    } //get_UnsafeInstanceCount

    /** @property
     */
    public static int get_SafeInstanceCount()
    {
        return safeInstanceCount;
    } //get_SafeInstanceCount

    public CountClass()
    {
        unsafeInstanceCount++;
        Interlocked.Increment(safeInstanceCount);
    } //CountClass

    public void finalize()
    {
        try {
            unsafeInstanceCount--;
            Interlocked.Decrement(safeInstanceCount);
            super.finalize();
        }
        catch (Throwable th) { 
        }
    } //finalize
}  //CountClass

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Show: