I need to try to lock on an object, and if its already locked just continue (after time out, or without it).
The C# lock statement is blocking.
Ed's got the right function for you. Just don't forget to call Monitor.Exit()
. You should use a try-finally
block to guarantee proper cleanup.
if (Monitor.TryEnter(someObject))
{
try
{
// use object
}
finally
{
Monitor.Exit(someObject);
}
}