Interlocked.Exchange Method (Int32%, Int32)

Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.

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

public static int Exchange(
	ref int location1,
	int value


Type: System.Int32%
The variable to set to the specified value.
Type: System.Int32
The value to which the location1 parameter is set.

Return Value

Type: System.Int32
The original value of location1.


The address of location1 is a null pointer.

The following example shows a thread-safe resource locking mechanism.

using System;
using System.Threading;

class Example
    private static System.Windows.Controls.TextBlock outputBlock;

    //0 for false, 1 for true.
    private static int usingResource = 0;

    private const int numThreadIterations = 5;
    private const int numThreads = 10;

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
        Example.outputBlock = outputBlock;

        // Run the demo on a separate thread because it uses Sleep, which makes
        // the user interface unresponsive.
        Thread ct = new Thread(ControlThread);

    private static void ControlThread()
        Thread myThread;
        Random rnd = new Random();

        int i;
        for(i = 0; i < numThreads; i++)
            myThread = new Thread(MyThreadProc);
            myThread.Name = String.Format("Thread{0}", i + 1);

            //Wait a random amount of time before starting next thread.
            Thread.Sleep(rnd.Next(0, 1000));

    private static void MyThreadProc()
        int i;
        for(i = 0; i < numThreadIterations; i++)

            //Wait 1 second before next attempt.

    //A simple method that denies reentrancy.
    public static bool UseResource()
        //0 indicates that the method is not in use.
        if (0 == Interlocked.Exchange(ref usingResource, 1))
            Output(String.Format("{0} acquired the lock\n", Thread.CurrentThread.Name));

            //Code to access a resource that is not thread safe would go here.
            //Simulate some work

            Output(String.Format("{0} exiting lock\n", Thread.CurrentThread.Name));

            //Release the lock
            Interlocked.Exchange(ref usingResource, 0);
            return true;
            Output(String.Format("   {0} was denied the lock\n", Thread.CurrentThread.Name));
            return false;

    // Helper methods:

    // In order to update the TextBlock object, which is on the UI thread, you must
    // make a cross-thread call by using the Dispatcher object that is associated 
    // with the TextBlock. The DisplayOutput helper method and its delegate, 
    // displayHelper, are used by the BeginInvoke method of the Dispatcher object
    // to append text to the TextBlock. 
    private static void Output(string msg)
        outputBlock.Dispatcher.BeginInvoke(displayHelper, msg);
    private static Action<string> displayHelper = new Action<string>(DisplayOutput);
    private static void DisplayOutput(string msg)
        outputBlock.Text += msg;

/* This example produces output similar to the following:

Thread1 acquired the lock
   Thread2 was denied the lock
Thread1 exiting lock 
Thread3 acquired the lock
   Thread4 was denied the lock
   Thread2 was denied the lock
   Thread1 was denied the lock
Thread3 exiting lock 
Thread6 acquired the lock
   Thread7 was denied the lock
   Thread4 was denied the lock
   Thread2 was denied the lock
   Thread5 was denied the lock
Thread6 exiting lock 


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions