tagged [locking]

Why are locks performed on separate objects?

Why are locks performed on separate objects? > [Difference between lock(locker) and lock(variable_which_I_am_using)](https://stackoverflow.com/questions/230716/difference-between-locklocker-and-lockv...

23 May at 11:59

Why doesn't C# allow a null value to be locked?

Why doesn't C# allow a null value to be locked? C# doesn't allow locking on a null value. I suppose I could check whether the value is null or not before I lock it, but because I haven't locked it ano...

29 Aug at 04:58

c# lock on reference passed to method - bad practice?

c# lock on reference passed to method - bad practice? I have a method similar to: A few points: 1. Is locking in this way bad practice? 2. Should I lock on a private static object instead? 3. If so, w...

How can I delete a file that is in use by another process?

How can I delete a file that is in use by another process? When I try to delete a file occurs the following exception: > The process cannot access the file '' because it is being used by another pro...

8 Mar at 12:53

Why is lock(this) {...} bad?

Why is lock(this) {...} bad? The [MSDN documentation](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/c5kehkcz(v=vs.110)) says that is "a problem if the instance ca...

19 Apr at 06:24

C# - Lock question using EnterWriteLock

C# - Lock question using EnterWriteLock The following code is from MSDN: ``` private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim(); private Dictionary innerCache = new Dictionary(); publ...

15 Aug at 21:36

How lock by method parameter?

How lock by method parameter? If DoSomething depend only on key, I want key dependent lock. I think it may be dictionary with sync objects. Is there any complete solution? Something like real example ...

23 May at 10:34

Destination Array not long enough?

Destination Array not long enough? I have a class with the following method: Which makes a copy of another list, `private List _bikes;` The strange thing now is, that I get the following error: > Dest...

15 Sep at 13:50

optimistic locking in ServiceStack's Redis Client

optimistic locking in ServiceStack's Redis Client We are trying to implement a pattern where we update the Redis in 2 cases 1. from the db every 5-10 minutes. 2. on specific use cases we update the cu...

yield returns within lock statement

yield returns within lock statement if i have a yield return in a lock statement does the lock get taken out on each yield (5 times in the example below) or only once for all the items in the list? Th...

How do I delete a file which is locked by another process in C#?

How do I delete a file which is locked by another process in C#? I'm looking for a way to delete a file which is locked by another process using C#. I suspect the method must be able to find which pro...

20 Aug at 23:34

Command-line tool for finding out who is locking a file

Command-line tool for finding out who is locking a file I would like to know who is locking a file (win32). I know about [WhoLockMe](http://www.dr-hoiby.com/WhoLockMe/), but I would like a which does ...

23 May at 12:9

How to properly lock a value type?

How to properly lock a value type? I was reading about threading and about locking. It is common practise that you can't (well should not) lock a value type. So the question is, what is the recommende...

7 Jan at 15:53

Volatile vs. Interlocked vs. lock

Volatile vs. Interlocked vs. lock Let's say that a class has a `public int counter` field that is accessed by multiple threads. This `int` is only incremented or decremented. To increment this field, ...

Lock() in a static method

Lock() in a static method I have a multi threaded application that writes to a settings xml file using a static method. I want to avoid that the file is being updated twice at the same time (causing a...

24 Oct at 21:27

How can I unit test a lock statement?

How can I unit test a lock statement? How would I write a unit test to ensure that a lock was acquired? For example: Is there a way to ensure that the `lock` statement is run? : I'm not trying to prov...

30 May at 13:27

Exceptions inside the lock block

Exceptions inside the lock block Say, if I have the following block on C# code: ``` public class SynchedClass { public void addData(object v) { lock(lockObject) { //Shall I worry abo...

7 Apr at 09:5

How to check if a table is locked in sql server

How to check if a table is locked in sql server I have a large report I am running on sql server. It takes several minutes to run. I don't want users clicking run twice. Since i wrap the whole procedu...

6 Oct at 06:9

What does a lock statement do under the hood?

What does a lock statement do under the hood? I see that for using objects which are not thread safe we wrap the code with a lock like this: So, what happens when multiple threads access the same code...

SQL Server - How to lock a table until a stored procedure finishes

SQL Server - How to lock a table until a stored procedure finishes I want to do this: Is something like that possible? Ultimately I want my SQL server reporting services report to call procedure A, a...

Does a locked object stay locked if an exception occurs inside it?

Does a locked object stay locked if an exception occurs inside it? In a c# threading app, if I were to lock an object, let us say a queue, and if an exception occurs, will the object stay locked? Here...

Difference between lock(this) and a lock on static object

Difference between lock(this) and a lock on static object Which of the following two code snippets is better to use? or `this` is an object of the current instance. So why is `lock (_locker)` always i...

How expensive is the lock statement?

How expensive is the lock statement? I've been experimenting with multi threading and parallel processing and I needed a counter to do some basic counting and statistic analysis of the speed of the pr...

Multiple lock objects necessary?

Multiple lock objects necessary? Given the following class: ``` class x { Object lockOne = new Object(); Object lockTwo = new Object(); List listOne = new List(); List listTwo = new List(); ...

11 Feb at 14:13

Using lock(obj) inside a recursive call

Using lock(obj) inside a recursive call As per my understanding a lock is not released until the runtime completes the code block of the lock(obj) ( because when the block completes it calls Monitor.E...

31 Oct at 09:34