Sto cercando di usare SpinLock, ma anche questo codice più fondamentale in un unico thread console app genera la seguente eccezione quando ho callSpinLock.Exit()SpinLock gettando SynchronizationLockException
System.Threading.SynchronizationLockException was unhandled by user code
Message=The calling thread does not hold the lock. Source=mscorlib
Ecco l'intero codice sorgente. ..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication48
{
class Program
{
static readonly SpinLock SpinLock = new SpinLock();
static void Main(string[] args)
{
bool lockTaken = false;
try
{
SpinLock.Enter(ref lockTaken);
if (lockTaken)
Console.WriteLine("Lock taken");
}
finally
{
if (lockTaken)
SpinLock.Exit();
}
Console.WriteLine("Done");
}
}
}
Il messaggio indica che il blocco non è di proprietà dello stesso thread che chiama Exit, ma dal codice è chiaramente. –