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

例外狀況

來電者沒有所需的權限。

範例

以下範例示範如何使用 BeginThreadAffinity and EndThreadAffinity 方法通知主機,該程式碼區塊依賴於實體作業系統執行緒的身份。

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 下的程式碼中使用此方法,則需在最高主機保護層級執行程式碼。

適用於

另請參閱