Lock thread using somthing other than a object
- by Scott Chamberlain
when using a lock does the thing you are locking on have to be a object. For example is this legal
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;
}
EDIT--
From reading SLaks' responce I know the above code would be not valid but would this be?
static MyClass myClass = new MyClass();
private static void DoCleanup()
{
lock (myClass)
{
//
}
return;
}