<p>I need to try to lock on an object, and if its already locked just continue (after time out, or without it).</p>

<p>The C# lock statement is blocking.</p>

[{'answer_id': 8553, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': 6, 'selected': True, 'text': '<p>I believe that you can use <a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.tryenter%28VS.71%29.aspx" rel="noreferrer"><code>Monitor.TryEnter()</code></a>. </p>\n\n<p>The lock statement just translates to a <code>Monitor.Enter()</code> call and a <code>try catch</code> block.</p>\n'}, {'answer_id': 8557, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 7, 'selected': False, 'text': "<p>Ed's got the right function for you.  Just don't forget to call <code>Monitor.Exit()</code>.  You should use a <code>try-finally</code> block to guarantee proper cleanup.</p>\n\n<pre><code>if (Monitor.TryEnter(someObject))\n{\n    try\n    {\n        // use object\n    }\n    finally\n    {\n        Monitor.Exit(someObject);\n    }\n}\n</code></pre>\n"}, {'answer_id': 8588, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 2, 'selected': False, 'text': '<p>You\'ll probably find this out for yourself now that the others have pointed you in the right direction, but TryEnter can also take a timeout parameter.</p>\n\n<p>Jeff Richter\'s "CLR Via C#" is an excellent book on details of CLR innards if you\'re getting into more complicated stuff.</p>\n'}, {'answer_id': 23728163, 'author': 'cwills', 'author_id': 256475, 'author_profile': 'https://Stackoverflow.com/users/256475', 'pm_score': 4, 'selected': False, 'text': '<p>I had the same problem, I ended up creating a class <code>TryLock</code> that implements IDisposable and then uses the <code>using</code> statement to control the scope of the lock:</p>\n\n<pre><code>public class TryLock : IDisposable\n{\n    private object locked;\n\n    public bool HasLock { get; private set; }\n\n    public TryLock(object obj)\n    {\n        if (Monitor.TryEnter(obj))\n        {\n            HasLock = true;\n            locked = obj;\n        }\n    }\n\n    public void Dispose()\n    {\n        if (HasLock)\n        {\n            Monitor.Exit(locked);\n            locked = null;\n            HasLock = false;\n        }\n    }\n}\n</code></pre>\n\n<p>And then use the following syntax to lock:</p>\n\n<pre><code>var obj = new object();\n\nusing (var tryLock = new TryLock(obj))\n{\n    if (tryLock.HasLock)\n    {\n        Console.WriteLine("Lock acquired..");\n    }\n}\n</code></pre>\n'}, {'answer_id': 42030543, 'author': 'David Burg', 'author_id': 890580, 'author_profile': 'https://Stackoverflow.com/users/890580', 'pm_score': 2, 'selected': False, 'text': '<p>Consider using AutoResetEvent and its method WaitOne with a timeout input.</p>\n\n<pre><code>static AutoResetEvent autoEvent = new AutoResetEvent(true);\nif(autoEvent.WaitOne(0))\n{\n    //start critical section\n    Console.WriteLine("no other thread here, do your job");\n    Thread.Sleep(5000);\n    //end critical section\n    autoEvent.Set();\n}\nelse\n{\n    Console.WriteLine("A thread working already at this time.");\n}\n</code></pre>\n\n<p>See <a href="https://msdn.microsoft.com/en-us/library/cc189907(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/cc189907(v=vs.110).aspx</a>\n<a href="https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(v=vs.110).aspx</a> and  <a href="https://msdn.microsoft.com/en-us/library/cc190477(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/cc190477(v=vs.110).aspx</a> </p>\n'}, {'answer_id': 71829467, 'author': 'Athanviel', 'author_id': 481626, 'author_profile': 'https://Stackoverflow.com/users/481626', 'pm_score': 0, 'selected': False, 'text': '<p>Based on <a href="https://stackoverflow.com/a/8557/481626">Dereks answer</a> a little helper method:</p>\n<pre><code>private bool TryExecuteLocked(object lockObject, Action action)\n{\n    if (!Monitor.TryEnter(lockObject))\n        return false;\n\n    try\n    {\n        action();\n    }\n    finally\n    {\n        Monitor.Exit(lockObject);\n    }\n\n    return true;\n}\n</code></pre>\n<p>Usage:</p>\n<pre><code>private object _myLockObject;\nprivate void Usage()\n{\n    if (TryExecuteLocked(_myLockObject, ()=&gt; DoCoolStuff()))\n    {\n        Console.WriteLine(&quot;Hurray!&quot;);\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/8546', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/195/']