Export (0) Print
Expand All

SynchronizationAttribute Class

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

For a list of all members of this type, see SynchronizationAttribute Members.

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

[Visual Basic]
<AttributeUsage(AttributeTargets.Class)>
<Serializable>
Public Class SynchronizationAttribute
   Inherits ContextAttribute
[C#]
[AttributeUsage(AttributeTargets.Class)]
[Serializable]
public class SynchronizationAttribute : ContextAttribute
[C++]
[AttributeUsage(AttributeTargets::Class)]
[Serializable]
public __gc class SynchronizationAttribute : public
   ContextAttribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class)
 Serializable
class SynchronizationAttribute extends ContextAttribute

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.

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

Example

[Visual Basic, C#, C++] 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 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

[C#] 
// 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;
    }
}

[C++] 
// Context-bound type with the Synchronization context attribute.
[Synchronization]
public __gc class SampleSyncronized : public ContextBoundObject 
{
    // A method that does some work, and returns the square of the given number.
public:
    int Square(int i) 
    {
        Console::Write(S"The hash of the thread executing ");
        Console::WriteLine(S"SampleSyncronized::Square is: {0}",
                    __box(Thread::CurrentThread->GetHashCode()));
        return i*i;
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.Remoting.Contexts

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

.NET Framework Security: 

See Also

SynchronizationAttribute Members | System.Runtime.Remoting.Contexts Namespace

Show:
© 2015 Microsoft