Thread.BeginThreadAffinity 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
通知主机托管代码将要执行依赖于当前物理操作系统线程的标识的指令。
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 ()
- 属性
例外
调用方没有所要求的权限。
示例
下面的示例演示如何使用 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 在释放锁后调用。
如果在 SQL Server 2005 下运行的代码中使用此方法,则需要在最高的主机保护级别上运行代码。