.NET Framework Class Library
SynchronizationAttribute Class

Enforces a synchronization domain for the current context and all contexts that share the same instance.

Namespace:  System.Runtime.Remoting.Contexts
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<AttributeUsageAttribute(AttributeTargets.Class)> _
<ComVisibleAttribute(True)> _
<SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags := SecurityPermissionFlag.Infrastructure)> _
<SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags := SecurityPermissionFlag.Infrastructure)> _
Public Class SynchronizationAttribute _
    Inherits ContextAttribute _
    Implements IContributeServerContextSink, IContributeClientContextSink
Visual Basic (Usage)
Dim instance As SynchronizationAttribute
C#
[SerializableAttribute]
[AttributeUsageAttribute(AttributeTargets.Class)]
[ComVisibleAttribute(true)]
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public class SynchronizationAttribute : ContextAttribute, 
    IContributeServerContextSink, IContributeClientContextSink
Visual C++
[SerializableAttribute]
[AttributeUsageAttribute(AttributeTargets::Class)]
[ComVisibleAttribute(true)]
[SecurityPermissionAttribute(SecurityAction::InheritanceDemand, Flags = SecurityPermissionFlag::Infrastructure)]
[SecurityPermissionAttribute(SecurityAction::LinkDemand, Flags = SecurityPermissionFlag::Infrastructure)]
public ref class SynchronizationAttribute : public ContextAttribute, 
    IContributeServerContextSink, IContributeClientContextSink
JScript
public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink
Remarks

When this attribute is applied to an object, only one thread can be executing in all contexts that share an instance of this property. This is done by contributing sinks that intercept and serialize incoming calls for the respective contexts. If the property is marked for reentry, then callouts are intercepted too. The callout interception allows other waiting threads to enter the synchronization domain for maximal throughput.

NoteNote:

There are two classes named SynchronizationAttribute : one in the System.Runtime.Remoting.Contexts namespace, and the other in the System.EnterpriseServices namespace. The System.EnterpriseServices..::.SynchronizationAttribute class supports only synchronous calls, and can be used only with serviced components. (For more information on serviced components, see [<topic://cpconservicedcomponentoverview>].) The System.Runtime.Remoting.Contexts..::.SynchronizationAttribute supports both synchronous and asynchronous calls, and can be used only with context bound objects. (For more information on context bound objects, see the ContextBoundObject class.)

NoteNote:

This class makes a link demand and an inheritance demand at the class level. A SecurityException is thrown when either the immediate caller or the derived class does not have infrastructure permission. For details about security demands, see Link Demands and Inheritance Demands.

Examples

The following code example demonstrates the use of the SynchronizationAttribute. For the complete example code, see the example for the AsyncResult class.

Visual Basic
' 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 
C#
// 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;
    }
}
Visual C++
// 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;
   }

};
.NET Framework Security

Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    System.Runtime.Remoting.Contexts..::.ContextAttribute
      System.Runtime.Remoting.Contexts..::.SynchronizationAttribute
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker