0

I have a function that looks like this

 public void DoSomething(){ var lockThis = new Object(); lock(lockThis){ //trying to step through code } } 

So I place a breakpoint within lock(lockThis){...}. In theory, only one thread is in the critical section at a time, but can't I step through? It breaks at the first line of code within it, and it keeps hitting the same line. The breakpoints behave just like outside the lock.

EDIT: I should describe the environment. This class and the method arewithin the middle tier DLL. The method is called by a webservice in an ASP.NET application. I am doing a stress test on the webservice which spawns multiple threads that call the above method.

1
  • Each thread is trying to lock a different object. Two threads can't lock the same lock at the same time, but these are different locks. Commented Mar 19, 2015 at 0:43

1 Answer 1

2

Your Lock object should be share between different thread, in this case you create single instance for each thread and call.`

Object lockThis = new Object(); 

or (depends on your code)

static Obejct lockThis = new Object(); public void DoSomething() { lock(lockThis) { //trying to step through code } } 
Sign up to request clarification or add additional context in comments.

5 Comments

I tried your second solution. It is still behaving the same. I edited the post to give a description of my environment.
As I said you should use same instance of object for locking, I don't know how you use this method. Do you use same instance of class or create new one each time?
Second one means Static object?
Yes. I am using the second option. Yes a new instance is created each time and I can't think of a way around it. Static class AND static method will be the way to go?
Yes the static object should create only once, and it should be working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.