2

I have got a method that i want only one user can access 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 and click the submit button on a asp.net webpage. I don't want other users access to be denied. They should be kept in some sort of queue and when method is finished serving one user next user should be served.

user3202862
  • 207
  • 4
  • 19
  • 2
    Do you mean it must be thread-safe? Use one of .NET synchronization mechanism you have, for example a lock (but we can't say from such fictional example, why do you have to do it? do you access a shared resource or a static variable?) – Adriano Repetti May 13 '14 at 12:03
  • 2
    Define "one user" in this context... because ASP.NET could be spanning multiple app-domains (during recycle), or multiple machines (a server farm) - how strict does this need to be? Can the same user make multiple concurrent http requests to the method? Does "user" here really mean "request"? And what about re-entrant calls? **What is it that we are trying to protect by restricting access?** (this is important context for the question; there is no need whatsoever to do anything with the method shown, as there is no shared state) – Marc Gravell May 13 '14 at 12:05
  • @Adriano yes i am using a shared resource and one user can be served at a time – user3202862 May 13 '14 at 12:07
  • @MarcGravell no the same user cannot make multiple concurrent requests to the method. – user3202862 May 13 '14 at 12:09
  • @user3202862 then probably just `lock` as per David's example – Marc Gravell May 13 '14 at 12:10
  • @MarcGravell what will happen if i lock and another user request same time – user3202862 May 13 '14 at 12:16
  • 1
    @user3202862 the thread attempting to acquire the lock will be blocked until the lock is released by the thread that currently has it (or more correctly: it will be blocked until it can successfully acquire the lock, if there are other competing threads) – Marc Gravell May 13 '14 at 12:18
  • @MarcGravell how will i be able to store other user request and call this method when this method finishes the first user – user3202862 May 13 '14 at 12:22
  • 1
    @user3202862 normally, you'd just let the lock do its job... – Marc Gravell May 13 '14 at 12:53

3 Answers3

4

Msdn got good explanation, check documentation. Thread Synchronization

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;
        }
    }

}

That 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.

Edit: Edited code for your method.

Dávid Kaya
  • 924
  • 4
  • 17
  • what will happen if another user send the request same time – user3202862 May 13 '14 at 12:14
  • @user3202862 Nothing, because the first thread locked that method, that means other threads won't be able to access that method in that time. – Dávid Kaya May 13 '14 at 12:17
  • how can i store other user data and then send that data to this method when it's free – user3202862 May 13 '14 at 12:24
  • @user3202862 the other requests will "block" until the one inside the lock is done, you don't need to save anything as the other requests will eventually go through just fine on their own (assuming your code inside the lock doesn't hang, of course) – Sven Grosen May 13 '14 at 12:29
  • Thanks alote for explaining process – user3202862 May 13 '14 at 12:48
1

you will need a static object, and use the keyword lock.

private static object locker = new object();
public static int add(int a, int b)
{
     lock(locker) 
     {
         //do stuff
     }
}
MozesM
  • 437
  • 3
  • 8
0

Assuming this method is within a singleton object you will then need to synchronize the method

Community
  • 1
  • 1
Mark Wagoner
  • 1,729
  • 1
  • 13
  • 20