.NET Framework Class Library
ThreadBeginThreadAffinity Method

Notifies a host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Sub BeginThreadAffinity
public static void BeginThreadAffinity()
public:
static void BeginThreadAffinity()
static member BeginThreadAffinity : unit -> unit
Exceptions
ExceptionCondition
SecurityException

The caller does not have the required permission.

Remarks

Some hosts of the common language runtime, such as Microsoft SQL Server 2005, provide their own thread management. A host that provides its own thread management can move an executing task from one physical operating system thread to another at any time. Most tasks are not affected by this switching. However, some tasks have thread affinity - that is, they depend on the identity of a physical operating system thread. These tasks must inform the host when they execute code that should not be switched.

For example, if your application calls a system API to acquire an operating system lock that has thread affinity, such as a Win32 CRITICAL_SECTION, you must call BeginThreadAffinity before acquiring the lock, and EndThreadAffinity after releasing the lock.

Using this method in code that runs under SQL Server 2005 requires the code to be run at the highest host protection level.

Examples

The following example demonstrates the use of the BeginThreadAffinity and EndThreadAffinity methods to notify a host that a block of code depends on the identity of a physical operating system thread.

Imports System.Threading
Imports System.Security.Permissions

<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
Friend Class MyUtility
    <SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)> _
    Public Sub PerformTask() 
        ' Code that does not have thread affinity goes here. 
        '
        Thread.BeginThreadAffinity()
        ' 
        ' Code that has thread affinity goes here. 
        '
        Thread.EndThreadAffinity()
        ' 
        ' More code that does not have thread affinity. 
    End Sub 'PerformTask
End Class 'MyUtility 
using System.Threading;
using System.Security.Permissions;

[SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlThread)]
public class MyUtility
{
    [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlThread)]
    public void PerformTask()
    {
        // Code that does not have thread affinity goes here. 
        //
        Thread.BeginThreadAffinity();
        // 
        // Code that has thread affinity goes here. 
        //
        Thread.EndThreadAffinity();
        // 
        // More code that does not have thread affinity.
    }
}
using namespace System::Threading;
using namespace System::Security::Permissions;

public ref class MyUtility
{
public:
   [SecurityPermissionAttribute(SecurityAction::Demand, ControlThread=true)]
   void PerformTask()
   {
      // Code that does not have thread affinity goes here. 
      //
      Thread::BeginThreadAffinity();
      // 
      // Code that has thread affinity goes here. 
      //
      Thread::EndThreadAffinity();
      // 
      // More code that does not have thread affinity.
   }
};
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
.NET Framework Security
  • SecurityCriticalAttribute 

    Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.