Thread.EndThreadAffinity Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Notifies a host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread.
public:
static void EndThreadAffinity();
public static void EndThreadAffinity ();
[System.Security.SecurityCritical]
public static void EndThreadAffinity ();
static member EndThreadAffinity : unit -> unit
[<System.Security.SecurityCritical>]
static member EndThreadAffinity : unit -> unit
Public Shared Sub EndThreadAffinity ()
- Attributes
The caller does not have the required permission.
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.
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.
}
};
using System.Threading;
public class MyUtility
{
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.
}
}
open System.Threading
let 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.
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
End Class
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.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: