---
title: "Make a method accessible to one user at a time"  
description: "Make a method accessible to one user at a time"  
author: "Anonymous User"  
published: 2014-11-27  
updated: 2014-11-27  
canonical: https://www.mindstick.com/forum/12699/make-a-method-accessible-to-one-user-at-a-time  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Make a method accessible to one user at a time

I have got a [method](https://www.mindstick.com/forum/166/webservice-method) that i want only one user can [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) at a time. I really don't know how to do this.

```
public static int add(int a, int b){ int c = a+b; return c;}
```

and I am calling this method when a user enters data into two [text boxes](https://www.mindstick.com/forum/34261/how-to-split-data-to-text-boxes-when-read-qr-code-data) and [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) the [submit button](https://www.mindstick.com/forum/12834/make-text-appear-when-submit-button-clicked-and-checkbox-is-unchecked) on a [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) webpage. I don't want other users access to be denied. They should be kept in some sort of [queue](https://www.mindstick.com/forum/160388/describe-the-basic-features-and-use-cases-of-the-queue-and-stack-collections-in-c-sharp) and when method is finished [serving](https://www.mindstick.com/forum/12893/how-to-serving-favicon-ico-in-asp-dot-net-mvc) one user next user should be served.

## Replies

### Reply by Pravesh Singh

Since you have a static method, you need static Object for locking.

```
public class Calculator{    private static System.Object lockThis = new System.Object();     public static void Add(int a, int b)    {           lock (lockThis)        {            return a+b;        }    } }
```

lock means, that whenever a Thread accesses that method and it is not locked, it will lock it and run the code. If it is locked it will do nothing until it is unlocked.


---

Original Source: https://www.mindstick.com/forum/12699/make-a-method-accessible-to-one-user-at-a-time

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
