Thread.EndThreadAffinity 方法

定义

通知主机托管代码已执行完依赖于当前物理操作系统线程的标识的指令。

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 ()
属性

例外

调用方没有所要求的权限。

示例

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

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;
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.
    }
}
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,则必须在获取锁之前和释放锁之后调用 。 BeginThreadAffinity EndThreadAffinity

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

适用于

另请参阅