Thread.BeginThreadAffinity 方法

定义

通知主机托管代码即将执行依赖于当前物理操作系统线程标识的说明。

public:
 static void BeginThreadAffinity();
public static void BeginThreadAffinity();
[System.Security.SecurityCritical]
public static void BeginThreadAffinity();
static member BeginThreadAffinity : unit -> unit
[<System.Security.SecurityCritical>]
static member BeginThreadAffinity : unit -> unit
Public Shared Sub BeginThreadAffinity ()
属性

例外

调用方没有所需的权限。

示例

以下示例演示如何使用 BeginThreadAffinityEndThreadAffinity 方法通知主机代码块依赖于物理操作系统线程的标识。

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

注解

公共语言运行时的某些主机(如 Microsoft SQL Server 2005)提供自己的线程管理。 提供自己的线程管理的主机可以随时将执行任务从一个物理操作系统线程移到另一个操作系统线程。 大多数任务不受此切换的影响。 但是,某些任务具有线程相关性,即它们依赖于物理操作系统线程的标识。 这些任务在执行不应切换的代码时,必须通知主机。

例如,如果应用程序调用系统 API 来获取具有线程相关性的操作系统锁(例如 Win32 CRITICAL_SECTION),则必须在获取锁之前以及EndThreadAffinity释放锁之后调用BeginThreadAffinity

在SQL Server 2005 下运行的代码中使用此方法要求在最高主机保护级别运行代码。

适用于

另请参阅