SynchronizationAttribute 类

为当前上下文和所有共享同一实例的上下文强制一个同步域。

**命名空间:**System.Runtime.Remoting.Contexts
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<AttributeUsageAttribute(AttributeTargets.Class)> _
<ComVisibleAttribute(True)> _
Public Class SynchronizationAttribute
    Inherits ContextAttribute
    Implements IContributeServerContextSink, IContributeClientContextSink
用法
Dim instance As SynchronizationAttribute
[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets.Class)] 
[ComVisibleAttribute(true)] 
public class SynchronizationAttribute : ContextAttribute, IContributeServerContextSink, IContributeClientContextSink
[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets::Class)] 
[ComVisibleAttribute(true)] 
public ref class SynchronizationAttribute : public ContextAttribute, IContributeServerContextSink, IContributeClientContextSink
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink
SerializableAttribute 
AttributeUsageAttribute(AttributeTargets.Class) 
ComVisibleAttribute(true) 
public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink

备注

将该属性应用于某个对象时,在共享该属性实例的所有上下文中只能执行一个线程。这是通过提供为各个上下文截获并序列化传入调用的接收器来实现的。如果为重入标记了该属性,那么标注也将被截获。标注侦听允许其他等待线程进入该同步域以获得最大的吞吐量。

提示

存在两个名为 SynchronizationAttribute 的类:一个在 System.Runtime.Remoting.Contexts 命名空间中,另一个在 System.EnterpriseServices 命名空间中。System.EnterpriseServices.SynchronizationAttribute 类仅支持同步调用,并且只可与接受服务的组件一起使用。(有关接受服务的组件的更多信息,请参见 服务组件概述。)System.Runtime.Remoting.Contexts.SynchronizationAttribute 同时支持同步调用和异步调用,并且只可与上下文绑定对象一起使用。(有关上下文绑定对象的更多信息,请参见 ContextBoundObject 类。)

提示

此类在类级别生成链接要求和继承要求。如果直接调用方或继承类没有基础结构权限,则会引发 SecurityException。有关安全要求的详细信息,请参见 链接要求继承要求

示例

下面的代码示例阐释了 SynchronizationAttribute 的用法。有关完整的代码示例,请参见 AsyncResult 类的示例。

' Context-bound type with the Synchronization context attribute.
<Synchronization()> Public Class SampleSyncronized
   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("SampleSyncronized.Square is: {0}", Thread.CurrentThread.GetHashCode())
      Return i * i
   End Function 'Square

End Class 'SampleSyncronized
// Context-bound type with the Synchronization context attribute.
[Synchronization()]
public class SampleSyncronized : 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("SampleSyncronized.Square is: {0}", 
                             Thread.CurrentThread.GetHashCode());
        return i*i;
    }
}
// Context-bound type with the Synchronization context attribute.

[Synchronization]
public ref class SampleSyncronized: 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( "SampleSyncronized::Square is: {0}", Thread::CurrentThread->GetHashCode() );
      return i * i;
   }

};
// Context-bound type with the Synchronization context attribute.
/** @attribute Synchronization()
 */
public class SampleSyncronized extends 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("SampleSyncronized.Square is: {0}",
            (Int32)System.Threading.Thread.get_CurrentThread().GetHashCode());
        return i * i;
    } //Square
} //SampleSyncronized

.NET Framework 安全性

继承层次结构

System.Object
   System.Attribute
     System.Runtime.Remoting.Contexts.ContextAttribute
      System.Runtime.Remoting.Contexts.SynchronizationAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

SynchronizationAttribute 成员
System.Runtime.Remoting.Contexts 命名空间