共用方式為


Interlocked.Exchange 方法

定義

將變數設定為指定的值做為不可部分完成的作業。

多載

Exchange(UInt16, UInt16)

將16位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(UInt32, UInt32)

將 32 位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Single, Single)

將單精度浮點數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(UIntPtr, UIntPtr)

將平臺特定的句柄或指標設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(UInt64, UInt64)

將 64 位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(SByte, SByte)

將8位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Byte, Byte)

將 8 位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(IntPtr, IntPtr)

將平臺特定的句柄或指標設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Int64, Int64)

將 64 位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Int32, Int32)

將 32 位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Int16, Int16)

將16位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Double, Double)

將雙精確度浮點數設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(Object, Object)

將物件設定為指定的值,並將原始對象的參考當做不可部分完成的作業傳回。

Exchange<T>(T, T)

將指定型別的變數 T 設定為指定的值,並以不可部分完成的作業傳回原始值。

Exchange(UInt16, UInt16)

重要

此 API 不符合 CLS 規範。

將16位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static System::UInt16 Exchange(System::UInt16 % location1, System::UInt16 value);
[System.CLSCompliant(false)]
public static ushort Exchange (ref ushort location1, ushort value);
[<System.CLSCompliant(false)>]
static member Exchange : uint16 * uint16 -> uint16
Public Shared Function Exchange (ByRef location1 As UShort, value As UShort) As UShort

參數

location1
UInt16

要設定為指定值的變數。

value
UInt16

設定 location1 參數的值。

傳回

location1的原始值。

屬性

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(UInt32, UInt32)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.cs

重要

此 API 不符合 CLS 規範。

將 32 位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static System::UInt32 Exchange(System::UInt32 % location1, System::UInt32 value);
[System.CLSCompliant(false)]
public static uint Exchange (ref uint location1, uint value);
[<System.CLSCompliant(false)>]
static member Exchange : uint32 * uint32 -> uint32
Public Shared Function Exchange (ByRef location1 As UInteger, value As UInteger) As UInteger

參數

location1
UInt32

要設定為指定值的變數。

value
UInt32

設定 location1 參數的值。

傳回

location1的原始值。

屬性

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(Single, Single)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.cs

將單精度浮點數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static float Exchange(float % location1, float value);
public static float Exchange (ref float location1, float value);
static member Exchange : single * single -> single
Public Shared Function Exchange (ByRef location1 As Single, value As Single) As Single

參數

location1
Single

要設定為指定值的變數。

value
Single

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

另請參閱

適用於

Exchange(UIntPtr, UIntPtr)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.cs

重要

此 API 不符合 CLS 規範。

將平臺特定的句柄或指標設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static UIntPtr Exchange(UIntPtr % location1, UIntPtr value);
[System.CLSCompliant(false)]
public static UIntPtr Exchange (ref UIntPtr location1, UIntPtr value);
[<System.CLSCompliant(false)>]
static member Exchange : unativeint * unativeint -> unativeint
Public Shared Function Exchange (ByRef location1 As UIntPtr, value As UIntPtr) As UIntPtr

參數

location1
UIntPtr

unativeint

要設定為指定值的變數。

value
UIntPtr

unativeint

設定 location1 參數的值。

傳回

UIntPtr

unativeint

location1的原始值。

屬性

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(UInt64, UInt64)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.cs

重要

此 API 不符合 CLS 規範。

將 64 位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static System::UInt64 Exchange(System::UInt64 % location1, System::UInt64 value);
[System.CLSCompliant(false)]
public static ulong Exchange (ref ulong location1, ulong value);
[<System.CLSCompliant(false)>]
static member Exchange : uint64 * uint64 -> uint64
Public Shared Function Exchange (ByRef location1 As ULong, value As ULong) As ULong

參數

location1
UInt64

要設定為指定值的變數。

value
UInt64

設定 location1 參數的值。

傳回

location1的原始值。

屬性

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(SByte, SByte)

重要

此 API 不符合 CLS 規範。

將8位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static System::SByte Exchange(System::SByte % location1, System::SByte value);
[System.CLSCompliant(false)]
public static sbyte Exchange (ref sbyte location1, sbyte value);
[<System.CLSCompliant(false)>]
static member Exchange : sbyte * sbyte -> sbyte
Public Shared Function Exchange (ByRef location1 As SByte, value As SByte) As SByte

參數

location1
SByte

要設定為指定值的變數。

value
SByte

設定 location1 參數的值。

傳回

location1的原始值。

屬性

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(Byte, Byte)

將 8 位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static System::Byte Exchange(System::Byte % location1, System::Byte value);
public static byte Exchange (ref byte location1, byte value);
static member Exchange : byte * byte -> byte
Public Shared Function Exchange (ByRef location1 As Byte, value As Byte) As Byte

參數

location1
Byte

要設定為指定值的變數。

value
Byte

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(IntPtr, IntPtr)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.cs

將平臺特定的句柄或指標設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static IntPtr Exchange(IntPtr % location1, IntPtr value);
public static IntPtr Exchange (ref IntPtr location1, IntPtr value);
static member Exchange : nativeint * nativeint -> nativeint
Public Shared Function Exchange (ByRef location1 As IntPtr, value As IntPtr) As IntPtr

參數

location1
IntPtr

nativeint

要設定為指定值的變數。

value
IntPtr

nativeint

設定 location1 參數的值。

傳回

IntPtr

nativeint

location1的原始值。

例外狀況

location1 的位址是 null 指標。

另請參閱

適用於

Exchange(Int64, Int64)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.CoreCLR.cs

將 64 位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static long Exchange(long % location1, long value);
public static long Exchange (ref long location1, long value);
static member Exchange : int64 * int64 -> int64
Public Shared Function Exchange (ByRef location1 As Long, value As Long) As Long

參數

location1
Int64

要設定為指定值的變數。

value
Int64

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

另請參閱

適用於

Exchange(Int32, Int32)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.CoreCLR.cs

將 32 位帶正負號的整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static int Exchange(int % location1, int value);
public static int Exchange (ref int location1, int value);
static member Exchange : int * int -> int
Public Shared Function Exchange (ByRef location1 As Integer, value As Integer) As Integer

參數

location1
Int32

要設定為指定值的變數。

value
Int32

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

location1 的位址是 null 指標。

範例

下列程式代碼範例顯示安全線程的資源鎖定機制。

using namespace System;
using namespace System::Threading;

const int numThreads = 10;
const int numThreadIterations = 5;
ref class MyInterlockedExchangeExampleClass
{
public:
   static void MyThreadProc()
   {
      for ( int i = 0; i < numThreadIterations; i++ )
      {
         UseResource();
         
         //Wait 1 second before next attempt.
         Thread::Sleep( 1000 );

      }
   }


private:
   //A simple method that denies reentrancy.
   static bool UseResource()
   {
      
      //0 indicates that the method is not in use.
      if ( 0 == Interlocked::Exchange( usingResource, 1 ) )
      {
         Console::WriteLine( " {0} acquired the lock", Thread::CurrentThread->Name );
         
         //Code to access a resource that is not thread safe would go here.
         //Simulate some work
         Thread::Sleep( 500 );
         Console::WriteLine( " {0} exiting lock", Thread::CurrentThread->Name );
         
         //Release the lock
         Interlocked::Exchange( usingResource, 0 );
         return true;
      }
      else
      {
         Console::WriteLine( " {0} was denied the lock", Thread::CurrentThread->Name );
         return false;
      }
   }


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

int main()
{
   Thread^ myThread;
   Random^ rnd = gcnew Random;
   for ( int i = 0; i < numThreads; i++ )
   {
      myThread = gcnew Thread( gcnew ThreadStart( MyInterlockedExchangeExampleClass::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 ) );
      myThread->Start();

   }
}
using System;
using System.Threading;

namespace InterlockedExchange_Example
{
    class MyInterlockedExchangeExampleClass
    {
        //0 for false, 1 for true.
        private static int usingResource = 0;

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

        static void Main()
        {
            Thread myThread;
            Random rnd = new Random();

            for(int i = 0; i < numThreads; i++)
            {
                myThread = new Thread(new ThreadStart(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));
                myThread.Start();
            }
        }

        private static void MyThreadProc()
        {
            for(int i = 0; i < numThreadIterations; i++)
            {
                UseResource();
            
                //Wait 1 second before next attempt.
                Thread.Sleep(1000);
            }
        }

        //A simple method that denies reentrancy.
        static bool UseResource()
        {
            //0 indicates that the method is not in use.
            if(0 == Interlocked.Exchange(ref usingResource, 1))
            {
                Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name);
            
                //Code to access a resource that is not thread safe would go here.
            
                //Simulate some work
                Thread.Sleep(500);

                Console.WriteLine("{0} exiting lock", Thread.CurrentThread.Name);
            
                //Release the lock
                Interlocked.Exchange(ref usingResource, 0);
                return true;
            }
            else
            {
                Console.WriteLine("   {0} was denied the lock", Thread.CurrentThread.Name);
                return false;
            }
        }
    }
}
Imports System.Threading

Namespace InterlockedExchange_Example
    Class MyInterlockedExchangeExampleClass
        '0 for false, 1 for true.
        Private Shared usingResource As Integer = 0

        Private Const numThreadIterations As Integer = 5
        Private Const numThreads As Integer = 10

        <MTAThread> _
        Shared Sub Main()
            Dim myThread As Thread
            Dim rnd As New Random()

            Dim i As Integer
            For i = 0 To numThreads - 1
                myThread = New Thread(AddressOf 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))
                myThread.Start()
            Next i
        End Sub

        Private Shared Sub MyThreadProc()
            Dim i As Integer
            For i = 0 To numThreadIterations - 1
                UseResource()

                'Wait 1 second before next attempt.
                Thread.Sleep(1000)
            Next i
        End Sub 

        'A simple method that denies reentrancy.
        Shared Function UseResource() As Boolean
            '0 indicates that the method is not in use.
            If 0 = Interlocked.Exchange(usingResource, 1) Then
                Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name)

                'Code to access a resource that is not thread safe would go here.
                'Simulate some work
                Thread.Sleep(500)

                Console.WriteLine("{0} exiting lock", Thread.CurrentThread.Name)

                'Release the lock
                Interlocked.Exchange(usingResource, 0)
                Return True
            Else
                Console.WriteLine("   {0} was denied the lock", Thread.CurrentThread.Name)
                Return False
            End If
        End Function 
    End Class 
End Namespace

另請參閱

適用於

Exchange(Int16, Int16)

將16位無符號整數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static short Exchange(short % location1, short value);
public static short Exchange (ref short location1, short value);
static member Exchange : int16 * int16 -> int16
Public Shared Function Exchange (ByRef location1 As Short, value As Short) As Short

參數

location1
Int16

要設定為指定值的變數。

value
Int16

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

適用於

Exchange(Double, Double)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.cs

將雙精確度浮點數設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
 static double Exchange(double % location1, double value);
public static double Exchange (ref double location1, double value);
static member Exchange : double * double -> double
Public Shared Function Exchange (ByRef location1 As Double, value As Double) As Double

參數

location1
Double

要設定為指定值的變數。

value
Double

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

另請參閱

適用於

Exchange(Object, Object)

來源:
Interlocked.cs
來源:
Interlocked.cs
來源:
Interlocked.CoreCLR.cs

將物件設定為指定的值,並將原始對象的參考當做不可部分完成的作業傳回。

public:
 static System::Object ^ Exchange(System::Object ^ % location1, System::Object ^ value);
public static object Exchange (ref object location1, object value);
public static object? Exchange (ref object? location1, object? value);
static member Exchange : obj * obj -> obj
Public Shared Function Exchange (ByRef location1 As Object, value As Object) As Object

參數

location1
Object

要設定為指定值的變數。

value
Object

設定 location1 參數的值。

傳回

location1的原始值。

例外狀況

location1 的位址是 null 指標。

location1 的位址是 null 指標。

備註

重要

從 .NET Framework 2.0 開始,Exchange<T>(T, T) 方法多載會提供參考型別的型別安全替代方案。 建議您呼叫它,而不是這個多載。

另請參閱

適用於

Exchange<T>(T, T)

來源:
Interlocked.CoreCLR.cs
來源:
Interlocked.CoreCLR.cs
來源:
Interlocked.CoreCLR.cs

將指定型別的變數 T 設定為指定的值,並以不可部分完成的作業傳回原始值。

public:
generic <typename T>
 where T : class static T Exchange(T % location1, T value);
public static T Exchange<T> (ref T location1, T value) where T : class;
[System.Runtime.InteropServices.ComVisible(false)]
public static T Exchange<T> (ref T location1, T value) where T : class;
static member Exchange : 'T * 'T -> 'T (requires 'T : null)
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Exchange : 'T * 'T -> 'T (requires 'T : null)
Public Shared Function Exchange(Of T As Class) (ByRef location1 As T, value As T) As T

類型參數

T

要用於 location1value的類型。 此類型必須是參考型別。

參數

location1
T

要設定為指定值的變數。 這是參考參數(在 C# 中ref,在 Visual Basic 中 ByRef)。

value
T

設定 location1 參數的值。

傳回

T

location1的原始值。

屬性

例外狀況

location1 的位址是 null 指標。

指定了不支援的 T

備註

這個方法僅支持參考型別。 Int32Int64IntPtrSingleDouble 實值型別都有 Exchange 方法的多載,但不支援其他實值型別。

注意

此方法多載最好是 Exchange(Object, Object) 方法多載,因為後者需要晚期綁定存取目的地物件 。

適用於