Thread.BeginThreadAffinity メソッド

定義

マネージド コードが現在の物理オペレーティング システム スレッドの ID に依存する命令を実行しようとしていることをホストに通知します。

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 メソッドを使用して、コード ブロックが物理オペレーティング システム スレッドの ID に依存していることをホストに通知する方法を示します。

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 など、共通言語ランタイムの一部のホストは、独自のスレッド管理を提供します。 独自のスレッド管理を提供するホストは、実行中のタスクをある物理オペレーティング システム スレッドから別のオペレーティング システム スレッドにいつでも移動できます。 ほとんどのタスクは、この切り替えの影響を受けません。 ただし、一部のタスクにはスレッド アフィニティがあります。つまり、物理オペレーティング システム スレッドの ID に依存します。 これらのタスクは、切り替えるべきでないコードを実行するときにホストに通知する必要があります。

たとえば、アプリケーションがシステム API を呼び出して、Win32 CRITICAL_SECTIONなどのスレッド アフィニティを持つオペレーティング システム ロックを取得する場合は、ロックを取得する前に BeginThreadAffinity を呼び出し、ロックを解放した後に EndThreadAffinity する必要があります。

SQL Server 2005 で実行されるコードでこのメソッドを使用するには、最も高いホスト保護レベルでコードを実行する必要があります。

適用対象

こちらもご覧ください