Share via


SynchronizationAttribute-Klasse

Erzwingt eine Synchronisierungsdomäne für den aktuellen Kontext und alle Kontexte, die dieselbe Instanz gemeinsam nutzen.

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

Syntax

'Declaration
<SerializableAttribute> _
<AttributeUsageAttribute(AttributeTargets.Class)> _
<ComVisibleAttribute(True)> _
Public Class SynchronizationAttribute
    Inherits ContextAttribute
    Implements IContributeServerContextSink, IContributeClientContextSink
'Usage
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

Hinweise

Wenn dieses Attribut auf ein Objekt angewendet wird, kann in allen Kontexten, die eine Instanz dieser Eigenschaft gemeinsam nutzen, nur ein Thread ausgeführt werden. Dies wird dadurch erreicht, dass Empfänger bereitgestellt werden, die eingehende Aufrufe für die jeweiligen Kontexte abfangen und serialisieren. Wenn Reentranz für die Eigenschaft markiert ist, werden ausgehende Aufrufe ebenfalls abgefangen. Durch das Abfangen von ausgehenden Aufrufen wird es anderen wartenden Threads ermöglicht, in die Synchronisierungsdomäne einzutreten, um einen maximalen Durchsatz zu erreichen.

Hinweis

Es sind zwei Klassen mit dem Namen SynchronizationAttribute vorhanden. Eine befindet sich im System.Runtime.Remoting.Contexts-Namespace und die andere im System.EnterpriseServices-Namespace. Die System.EnterpriseServices.SynchronizationAttribute-Klasse unterstützt nur synchrone Aufrufe und kann nur bei Serviced Components verwendet werden. (Weitere Informationen zu Serviced Components finden Sie unter Übersicht über ServicedComponents.) System.Runtime.Remoting.Contexts.SynchronizationAttribute unterstützt sowohl synchrone als auch asynchrone Aufrufe und kann nur bei kontextgebundenen Objekten verwendet werden. (Weitere Informationen zu kontextgebundenen Objekten finden Sie unter der ContextBoundObject-Klasse.)

Hinweis

Diese Klasse führt einen Verknüpfungsaufruf und eine Vererbungsforderung auf der Klassenebene aus. Eine SecurityException wird ausgelöst, wenn der direkte Aufrufer bzw. die abgeleitete Klasse nicht über die Berechtigung für die Infrastruktur verfügen. Ausführliche Informationen über Sicherheitsforderungen finden Sie unter Verknüpfungsaufrufe und Vererbungsforderungen.

Beispiel

Im folgenden Codebeispiel wird die Verwendung von SynchronizationAttribute veranschaulicht. Den vollständigen Beispielcode finden Sie im Beispiel zur AsyncResult-Klasse.

' 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-Sicherheit

Vererbungshierarchie

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

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

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 unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

SynchronizationAttribute-Member
System.Runtime.Remoting.Contexts-Namespace