locked
Execute one method a time per user RRS feed

  • Question

  • Hi!

    I need to make a button execute just one method call per user, i need to be per user, because my scenario is in on a asp web site.

    User A -> button1 -> (lets suppose this button needs 10 sec to execute) execut function F;
    User A -> button1 before 10 sec -> show function F is executing;

    User B -> press button1 at same time of User A - execute function F;

    How can i do that?


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Friday, June 13, 2014 3:08 PM

Answers

  • Hi,

    You could start by just implementing something on the client, that disables the button until the final response from the server is returned. This should be pretty easy, because from the client perspective, there is only one user to worry about. Depending on the nature of you application, this might be sufficient.

    If you additionally wish to enforce this on the server, you need a per user flag, to act as a gate on the function. For example, in ASP.NET, you could put this flag in the Session property bag, which is per user (I'm not a massive fan of Session[], but for a short lived Boolean flag, its appropriate).

    You don't mention which framework/technologies you are using, but a more detailed answer would require that information.

    Regards,

    Nick

    • Marked as answer by Anne Jing Sunday, June 22, 2014 9:03 AM
    Friday, June 13, 2014 4:24 PM
  • You can try Monitor.TryEnter in this Scenario

    You can use User related object "object" parameter for TryEnter 

    Monitor.TryEnter


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    • Marked as answer by Anne Jing Sunday, June 22, 2014 9:03 AM
    Friday, June 13, 2014 7:12 PM
  • You could use a ConcurrentDictionary<TKey, TValue> where the TKey represents some kind of user identification (such as a UserId) and the TValue represents a lock object:

     class ThreadClass
     {
      private readonly ConcurrentDictionary<string, object> dict = new ConcurrentDictionary<string, object>();
      public void Access(string username)
      {
       object lockObject = dict.GetOrAdd(username, new object());
       lock (lockObject)
       {
        //do something...
       }
      }
     }
    

    • Marked as answer by Anne Jing Sunday, June 22, 2014 9:03 AM
    Tuesday, June 17, 2014 1:06 PM

All replies

  • http://en.wikipedia.org/wiki/Singleton_pattern

    http://msdn.microsoft.com/en-us/library/ff650316.aspx

    http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

    There is also the ASP.NET forum.

    http://forums.asp.net/

    You may also have to look into using some kind of application seetion object for your needs possibly.

    Friday, June 13, 2014 3:50 PM
  • http://en.wikipedia.org/wiki/Singleton_pattern

    http://msdn.microsoft.com/en-us/library/ff650316.aspx

    http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

    There is also the ASP.NET forum.

    http://forums.asp.net/

    You may also have to look into using some kind of application seetion object for your needs possibly.

    I known asp forum, but i was thinking that my solution will be more C# related.

    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Friday, June 13, 2014 4:04 PM
  • Hi,

    You could start by just implementing something on the client, that disables the button until the final response from the server is returned. This should be pretty easy, because from the client perspective, there is only one user to worry about. Depending on the nature of you application, this might be sufficient.

    If you additionally wish to enforce this on the server, you need a per user flag, to act as a gate on the function. For example, in ASP.NET, you could put this flag in the Session property bag, which is per user (I'm not a massive fan of Session[], but for a short lived Boolean flag, its appropriate).

    You don't mention which framework/technologies you are using, but a more detailed answer would require that information.

    Regards,

    Nick

    • Marked as answer by Anne Jing Sunday, June 22, 2014 9:03 AM
    Friday, June 13, 2014 4:24 PM
  • You can try Monitor.TryEnter in this Scenario

    You can use User related object "object" parameter for TryEnter 

    Monitor.TryEnter


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    • Marked as answer by Anne Jing Sunday, June 22, 2014 9:03 AM
    Friday, June 13, 2014 7:12 PM
  • hi, Have you tried the above?

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Tuesday, June 17, 2014 11:08 AM
  • You could use a ConcurrentDictionary<TKey, TValue> where the TKey represents some kind of user identification (such as a UserId) and the TValue represents a lock object:

     class ThreadClass
     {
      private readonly ConcurrentDictionary<string, object> dict = new ConcurrentDictionary<string, object>();
      public void Access(string username)
      {
       object lockObject = dict.GetOrAdd(username, new object());
       lock (lockObject)
       {
        //do something...
       }
      }
     }
    

    • Marked as answer by Anne Jing Sunday, June 22, 2014 9:03 AM
    Tuesday, June 17, 2014 1:06 PM