Thread.EndThreadAffinity 方法

定義

通知主機 Managed 程式碼已完成執行指令,而這些指令相依於目前實體作業系統執行緒的識別。

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

備註

common language runtime 的某些主控制項(例如 Microsoft SQL Server 2005)提供自己的執行緒管理。 提供自己的執行緒管理的主機可以在任何時間將執行中的工作從一個實體作業系統執行緒移至另一個。 大部分的工作都不會受到此切換的影響。 不過,某些工作具有線程親和性,也就是它們依存于實體作業系統執行緒的識別。 這些工作在執行不應切換的程式碼時,必須通知主機。

例如,如果您的應用程式呼叫系統 API 來取得具有線程親和性的作業系統鎖定(例如 Win32 CRITICAL_SECTION),則您必須在取得 BeginThreadAffinity 鎖定之前呼叫,並 EndThreadAffinity 在釋放鎖定之後呼叫。

在 SQL Server 2005 下執行的程式碼中使用這個方法需要以最高的主機保護層級執行程式碼。

適用於

另請參閱