SynchronizationAttribute Class

Definition

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

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
Inheritance
SynchronizationAttribute
Attributes
Implements

Examples

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

// 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

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.

Note

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. 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.)

Note

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.

Constructors

SynchronizationAttribute()

Initializes a new instance of the SynchronizationAttribute class with default values.

SynchronizationAttribute(Boolean)

Initializes a new instance of the SynchronizationAttribute class with a Boolean value indicating whether reentry is required.

SynchronizationAttribute(Int32)

Initializes a new instance of the SynchronizationAttribute class with a flag indicating the behavior of the object to which this attribute is applied.

SynchronizationAttribute(Int32, Boolean)

Initializes a new instance of the SynchronizationAttribute class with a flag indicating the behavior of the object to which this attribute is applied, and a Boolean value indicating whether reentry is required.

Fields

AttributeName

This API supports the product infrastructure and is not intended to be used directly from your code.

Indicates the name of the context attribute.

(Inherited from ContextAttribute)
NOT_SUPPORTED

Indicates that the class to which this attribute is applied cannot be created in a context that has synchronization. This field is constant.

REQUIRED

Indicates that the class to which this attribute is applied must be created in a context that has synchronization. This field is constant.

REQUIRES_NEW

Indicates that the class to which this attribute is applied must be created in a context with a new instance of the synchronization property each time. This field is constant.

SUPPORTED

Indicates that the class to which this attribute is applied is not dependent on whether the context has synchronization. This field is constant.

Properties

IsReEntrant

Gets or sets a Boolean value indicating whether reentry is required.

Locked

Gets or sets a Boolean value indicating whether the Context implementing this instance of SynchronizationAttribute is locked.

Name

This API supports the product infrastructure and is not intended to be used directly from your code.

Gets the name of the context attribute.

(Inherited from ContextAttribute)
TypeId

When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Methods

Equals(Object)

This API supports the product infrastructure and is not intended to be used directly from your code.

Returns a Boolean value indicating whether this instance is equal to the specified object.

(Inherited from ContextAttribute)
Freeze(Context)

This API supports the product infrastructure and is not intended to be used directly from your code.

Called when the context is frozen.

(Inherited from ContextAttribute)
GetClientContextSink(IMessageSink)

Creates a CallOut sink and chains it in front of the provided chain of sinks at the context boundary on the client end of a remoting call.

GetHashCode()

This API supports the product infrastructure and is not intended to be used directly from your code.

Returns the hashcode for this instance of ContextAttribute.

(Inherited from ContextAttribute)
GetPropertiesForNewContext(IConstructionCallMessage)

Adds the Synchronized context property to the specified IConstructionCallMessage.

GetServerContextSink(IMessageSink)

Creates a synchronized dispatch sink and chains it in front of the provided chain of sinks at the context boundary on the server end of a remoting call.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
IsContextOK(Context, IConstructionCallMessage)

Returns a Boolean value indicating whether the context parameter meets the context attribute's requirements.

IsDefaultAttribute()

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
IsNewContextOK(Context)

This API supports the product infrastructure and is not intended to be used directly from your code.

Returns a Boolean value indicating whether the context property is compatible with the new context.

(Inherited from ContextAttribute)
Match(Object)

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to