1

Here is my class:

public class Server
{
    public string Name{get;set;}
    public void DoTheJob()
    {
        //MoreCode
    }
}

I have created a new instance of Server and I want to make the instance inaccessible while DoTheJob() is running.

Can I do something like this?

DoTheJob()
{
    lock(this)
    {
        //logic
    }
}
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
Dilshod
  • 3,189
  • 3
  • 36
  • 67
  • Are you trying to lock others from reading e.g. `Name` or just avoid two parallel executions of `DoTheJob`? – Christoph Fink Apr 28 '14 at 09:09
  • what do you mean by inaccessible? It should just wait for the first thread or it should throw an exception? – Sergey Litvinov Apr 28 '14 at 09:10
  • @chrfin avoid parallel execution. For example server can serve only for one person at a time. – Dilshod Apr 28 '14 at 09:10
  • then you can handle it in your code by using some `bool` variable and update it on method begin\end. if this variable is `true`, then you can throw an exception or just return empty result – Sergey Litvinov Apr 28 '14 at 09:12
  • @SergeyLitvinov it should wait for the first thread. – Dilshod Apr 28 '14 at 09:12
  • Ok, then you can do it with lock, or you can also try to use `[MethodImpl(MethodImplOptions.Synchronized)]` on the method like in this question - http://stackoverflow.com/questions/6140048/difference-between-manual-locking-and-synchronized-methods and http://stackoverflow.com/questions/541194/c-sharp-version-of-javas-synchronized-keyword. But as for me lock would be better as it will generate very similar code – Sergey Litvinov Apr 28 '14 at 09:13
  • @SergeyLitvinov is the example I wrote works for that? – Dilshod Apr 28 '14 at 09:14
  • 1
    it should. But you need to remember that it uses `this` for locking. and if you will use `this` again on this method, then it can cause dead lock – Sergey Litvinov Apr 28 '14 at 09:16
  • @SergeyLitvinov ok. I was afraid of that. – Dilshod Apr 28 '14 at 09:17
  • 1
    Also note that you're only going to lock out other code also locking on the same object. Using `lock` does not magically make other properties and methods wait for the lock to be released, they need to lock as well. Basically, the fact that you've locked the door is a way for you to cooperate with other people wanting to get in. However, if someone else is either already in the house, or goes in through the window, the locked door makes no difference. So make sure all the code you want to wait is in fact locking as well. – Lasse V. Karlsen Apr 28 '14 at 09:35

1 Answers1

1

Yes you can, but you should not lock on this to avoid deadlocks. Use something like:

public class Server
{
    private object lockObject = new object();

    public string Name { get; set; }
    public void DoTheJob()
    {
        lock(lockObject)
        {
            //MoreCode
        }
    }
}

If you also want to avoid the reading of Name you would need to implement a getter and setter which also use the same lock.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113