共用方式為


SynchronizationAttribute 類別

定義

強制執行當前上下文及所有共享同一實例的上下文的同步域。

public ref class SynchronizationAttribute : System::Runtime::Remoting::Contexts::ContextAttribute, System::Runtime::Remoting::Contexts::IContributeClientContextSink, System::Runtime::Remoting::Contexts::IContributeServerContextSink
[System.AttributeUsage(System.AttributeTargets.Class)]
[System.Serializable]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink
[System.AttributeUsage(System.AttributeTargets.Class)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink
[System.AttributeUsage(System.AttributeTargets.Class)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink
[<System.AttributeUsage(System.AttributeTargets.Class)>]
[<System.Serializable>]
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeServerContextSink
    interface IContributeClientContextSink
[<System.AttributeUsage(System.AttributeTargets.Class)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeServerContextSink
    interface IContributeClientContextSink
[<System.AttributeUsage(System.AttributeTargets.Class)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeServerContextSink
    interface IContributeClientContextSink
Public Class SynchronizationAttribute
Inherits ContextAttribute
Implements IContributeClientContextSink, IContributeServerContextSink
繼承
SynchronizationAttribute
屬性
實作

範例

以下程式碼範例展示了 的使用 SynchronizationAttribute。 完整範例程式碼請參閱該 AsyncResult 類別範例。

// Context-bound type with the Synchronization context attribute.

[Synchronization]
public ref class SampleSynchronized: public ContextBoundObject
{
public:

   // A method that does some work, and returns the square of the given number.
   int Square( int i )
   {
      Console::Write( "The hash of the thread executing " );
      Console::WriteLine( "SampleSynchronized::Square is: {0}", Thread::CurrentThread->GetHashCode() );
      return i * i;
   }

};
// Context-bound type with the Synchronization context attribute.
[Synchronization()]
public class SampleSynchronized : ContextBoundObject {

    // A method that does some work, and returns the square of the given number.
    public int Square(int i)  {

        Console.Write("The hash of the thread executing ");
        Console.WriteLine("SampleSynchronized.Square is: {0}",
                             Thread.CurrentThread.GetHashCode());
        return i*i;
    }
}
' Context-bound type with the Synchronization context attribute.
<Synchronization()> Public Class SampleSynchronized
   Inherits ContextBoundObject
   
   ' A method that does some work, and returns the square of the given number.
   Public Function Square(i As Integer) As Integer
      
      Console.Write("The hash of the thread executing ")
      Console.WriteLine("SampleSynchronized.Square is: {0}", Thread.CurrentThread.GetHashCode())
      Return i * i
   End Function 

End Class

備註

當此屬性套用於物件時,所有共享此屬性實例的上下文中,只能執行一個執行緒。 這是透過貢獻匯(sinks)來攔截並序列化來電,針對各自的上下文來實現的。 如果該物業標示為可重返大氣層,呼叫也會被攔截。 呼叫攔截允許其他等待的執行緒進入同步領域,以達到最大吞吐量。

備註

有兩個類別: SynchronizationAttribute 一個在命名空間中 System.Runtime.Remoting.Contexts 命名,另一個在 System.EnterpriseServices 命名空間中。 此 System.EnterpriseServices.SynchronizationAttribute 類別僅支援同步呼叫,且僅可用於服務元件。 支援 System.Runtime.Remoting.Contexts.SynchronizationAttribute 同步與非同步呼叫,且僅能用於上下文綁定物件。 (欲了解更多關於上下文綁定物件的資訊,請參見類別。 ContextBoundObject

備註

此類別在類別層級提出連結需求與繼承需求。 當直接呼叫者或衍生類別沒有基礎設施權限時,會拋出 A SecurityException 。 如需安全性需求的詳細資訊,請參閱 連結需求

建構函式

名稱 Description
SynchronizationAttribute()

初始化一個以預設值的新類別實例 SynchronizationAttribute

SynchronizationAttribute(Boolean)

初始化一個新類別實例 SynchronizationAttribute ,並設定一個布林值,表示是否需要重新輸入。

SynchronizationAttribute(Int32, Boolean)

初始化一個新的類別實例 SynchronizationAttribute ,並以旗標表示該屬性所應用物件的行為,並以布林值表示是否需要重新進入。

SynchronizationAttribute(Int32)

初始化該類別的新實例 SynchronizationAttribute ,並以旗標表示該屬性所應用物件的行為。

欄位

名稱 Description
AttributeName

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

表示上下文屬性的名稱。

(繼承來源 ContextAttribute)
NOT_SUPPORTED

表示該屬性所應用的類別無法在有同步的上下文中建立。 此欄位是常數。

REQUIRED

表示該屬性所應用的類別必須在有同步的上下文中建立。 此欄位是常數。

REQUIRES_NEW

表示該屬性所應用的類別必須在每次都帶有新同步屬性實例的上下文中建立。 此欄位是常數。

SUPPORTED

表示該屬性所應用的類別不依賴於上下文是否有同步。 此欄位是常數。

屬性

名稱 Description
IsReEntrant

取得或設定一個布林值,表示是否需要重入。

Locked

取得或設定一個布林值,表示實作 的SynchronizationAttribute實作是否Context被鎖定。

Name

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

會取得上下文屬性的名稱。

(繼承來源 ContextAttribute)
TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

回傳一個布林值,表示該實例是否等於指定物件。

(繼承來源 ContextAttribute)
Freeze(Context)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

當情境凍結時才會被召喚。

(繼承來源 ContextAttribute)
GetClientContextSink(IMessageSink)

建立一個 CallOut sink,並將其串接在用戶端端上下文邊界的 sink 鏈前面。

GetHashCode()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

回傳此實例 ContextAttribute的雜湊碼。

(繼承來源 ContextAttribute)
GetPropertiesForNewContext(IConstructionCallMessage)

Synchronized 上下文屬性加入指定的 IConstructionCallMessage

GetServerContextSink(IMessageSink)

建立同步派遣匯入,並將其串接在伺服器端上下文邊界的接收鏈前方。

GetType()

取得目前實例的 Type

(繼承來源 Object)
IsContextOK(Context, IConstructionCallMessage)

回傳一個布林值,表示上下文參數是否符合上下文屬性的要求。

IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
IsNewContextOK(Context)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

回傳一個布林值,表示上下文屬性是否與新上下文相容。

(繼承來源 ContextAttribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於