---
title: "TimedLock class: helping find and avoid deadlocks in your multithreaded code"
url: "https://osherove.com/blog/2005/4/8/timedlock-class-helping-find-and-avoid-deadlocks-in-your-mul.md"
canonical: "https://osherove.com/blog/2005/4/8/timedlock-class-helping-find-and-avoid-deadlocks-in-your-mul.html"
published: "2005-04-08T10:20:00"
updated: "2005-04-08T10:20:00"
tags: [".Net Quotations"]
author: "Roy Osherove"
description: "Another gem I found through Eric's blog is the TimedLock class. It helps in locking and detecting the offending code in multi threading deadlock issues (at least makes it not as painful). Originally posted by IanG (and…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# TimedLock class: helping find and avoid deadlocks in your multithreaded code

By Roy Osherove April 8, 2005 In [.Net Quotations](https://osherove.com/blog/category/net-quotations)

**Historical article** · Originally published April 8, 2005; technical details may be outdated.

Another gem I found through [Eric's blog](http://www.randomtree.org/eric/techblog/archives/2004/10/multithreading_is_hard.html) is the TimedLock class. It helps in locking and detecting the offending code in multi threading deadlock issues (at least makes it not as painful). [Originally posted by IanG](http://www.interact-sw.co.uk/iangblog/2004/03/23/locking) (and then [republished](http://www.interact-sw.co.uk/iangblog/2004/04/26/yetmoretimedlocking)), It [was reintroduced and enhanced by Phill Haack](http://haacked.com/archive/2004/10/13/1341.aspx) (which looks to be the version you should be using to get a wonderful thing like a stack trace for a deadlocked thread...).

So - here's the recipe you should be following.. I think.

1.You should read the "[why" on this class here](http://www.interact-sw.co.uk/iangblog/2004/03/23/locking)

2\. Then use [the one found here](http://haacked.com/archive/2004/10/13/1341.aspx) 

3\. But it's important that you fix the small bug in the class [as is prescribed here](http://www.randomtree.org/eric/techblog/archives/2004/10/multithreading_is_hard.html)! :).

"_The `TimedLock.Lock` method can be called to lock an object. It returns an object that implements `IDisposable`. (As it happens, this is an instance of the `TimedLock` class, but it doesn't really matter what it is - a private nested class could also have been used.) Now we can write code like this:_

```
using (TimedLock.Lock(obj)) { } 
```

_This is a very similar usage model to using the `lock` keyword - we rely on a compiler-generated `finally` block to call `Monitor.Exit`. It's just a bit less direct - the generated block calls `Dispose` on the `TimedLock`, which in turn calls `Monitor.Exit`. But now, if the monitor cannot be acquired (e.g., because of a deadlock) the code will throw an exception after ten seconds instead of blocking for ever. We get pretty close to the succinctness of the `lock` keyword, but we get to use `Monitor.TryEnter`."_

## More in .Net Quotations

[All posts in .Net Quotations](https://osherove.com/blog/category/net-quotations)

- Apr 9, 2005

    ### [Enterprise Library - Configuration AB Memory Leak Patch](https://osherove.com/blog/2005/4/9/enterprise-library-configuration-ab-memory-leak-patch.html)

- Apr 8, 2005

    ### [\[Free eBook\]FIT for Developing Software: Framework for Integrated Tests](https://osherove.com/blog/2005/4/8/free-ebookfit-for-developing-software-framework-for-integrat.html)

- Apr 8, 2005

    ### [Defensive event publishing, a different approach](https://osherove.com/blog/2005/4/8/defensive-event-publishing-a-different-approach.html)

- Apr 6, 2005

    ### [Solving Binary serialization versioning conflicts - The HashTable problem](https://osherove.com/blog/2005/4/6/solving-binary-serialization-versioning-conflicts-the-hashta.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
