共用方式為


SpinLock(Boolean) 建構函式

定義

使用可追蹤執行緒 ID 以改善偵錯的選項,初始化 SpinLock 結構的新執行個體。

public:
 SpinLock(bool enableThreadOwnerTracking);
public SpinLock (bool enableThreadOwnerTracking);
new System.Threading.SpinLock : bool -> System.Threading.SpinLock
Public Sub New (enableThreadOwnerTracking As Boolean)

參數

enableThreadOwnerTracking
Boolean

是否要擷取並使用執行緒 ID 以進行偵錯。

範例

下列範例示範如何使用 SpinLock。

// C#  
public class MyType  
{  
    private SpinLock _spinLock = new SpinLock();  

    public void DoWork()  
    {  
        bool lockTaken = false;   
        try  
        {  
           _spinLock.Enter(ref lockTaken);  
           // do work here protected by the lock  
        }  
        finally  
        {  
            if (lockTaken) _spinLock.Exit();  
        }  
    }  
}  
' Visual Basic  

Class MyType  
   Private _spinLock As New SpinLock()  

   Public Sub DoWork()  
      Dim lockTaken As Boolean = False  
      Try  
         _spinLock.Enter(lockTaken)  
         ' do work here protected by the lock  
      Finally  
         If lockTaken Then _spinLock.Exit()  
      End Try  
   End Sub  
End Class  

備註

追蹤執行緒擁有權的 SpinLock 無參數建構函式。

適用於

另請參閱