Export (0) Print
Expand All

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)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Class)] 
public class SynchronizationAttribute : ContextAttribute, IContributeServerContextSink, IContributeClientContextSink
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class) */ 
public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink
SerializableAttribute 
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Class) 
public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink
Not applicable.

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 Serviced Component Overview.) 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.

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 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.
/** @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

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

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

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft