使用锁定时,锁定的对象必须是对象。例如,这是合法的使用除锁定对象以外的其他锁定线程
static DateTime NextCleanup = DateTime.Now;
const TimeSpan CleanupInterval = new TimeSpan(1, 0, 0);
private static void DoCleanup()
{
lock ((object)NextCleanup)
{
if (NextCleanup < DateTime.Now)
{
NextCleanup = DateTime.Now.Add(CleanupInterval);
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(cleanupThread));
}
}
return;
}
这是第一个很好的解释,为什么你应该使用一个单独的对象进行锁定,而不是你计划使用的对象。 – 2010-05-05 18:33:08