locked
avoid variable collision RRS feed

  • Question

  • User-1389429259 posted

    Hi to all,

    I am using application.lock to make the application work for just one user at a time and I'm using sessionstate to keep their values so that they don't get mixed. Now, if the application is locked and another user tryes to starts the lock... what happens, will the second user wait until the first lock is released? and what if the first session hangs up and never releases the application? In a few words, the application connects to some webservices, receives some values and writes them into a database, a lot of values are coming from controls on the page that users interact with and the results have to be precice, I don't want the name of the first user to appear in the database field name of the second user and so on.

    Can I use a backgroundworker like in visual basic to make the application work in parallel for all the users to avoid the need of application.lock? And if the worker is not a solution, how to make a more complex and reliable application.lock to make sure that every user succeds on running the application from start to end?

     

    Thank you

    Friday, October 7, 2011 4:50 AM

Answers

  • User-1856974186 posted

    I don't know about a project, but you could create a class that contains properties for each item per user, then store that in the application, using the user name as a key. For example:

    public class UserProperties
    {
      public string Name { get;set; }
      public int Age { get;set; }
      ..
    }

    Then in your application you create an instance of this, set the properties and store it in the application:

    UserProperties props = new UserProperties();
    props.Name = "Dave";
    props.Age = 8;

    Application[User.Identity.Name] = props;

    Then to access it:

    UserProperties props = Application[User.Identity.Name] as UserProperies


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 17, 2011 3:56 AM

All replies

  • User1610201420 posted

    I don't want the name of the first user to appear in the database field name of the second user and so on.

    I think every user have its own session, why collision? when save the imformation to the database, check the userid is enough i think.

    And why you need to lock the app? What do you want to protect?

    Framework is hard to say.

    I think you can try this in some simple way.

    Monday, October 10, 2011 4:02 AM
  • User-1856974186 posted

    The second thread will wait until the lock is released. If it's never released the second thread will timeout.

    Anything that accesses a web service should ideally be done asynchronously; you've no guarantee it will ever return, so by using synchronous access you're introducing a potential wait already.

    What's your scenario? What is it you're trying to achieve?

    Monday, October 10, 2011 4:07 AM
  • User-1389429259 posted

    Hi and thanks for your answers,

    the scenario is quite complex. I have 1108 files spread into classes and hundreds of variables. The persons navigate to my website, there is no login or register or anything. The user selects and writes into textboxes the information that is only for him and then submits. When controls are changed, a lot of variables are changed also, and if 2 or more users are at this step of the application, some variables may get mixed. Then, the application connects to 10 webservices, sends information from xml files and datasets (here I need syncronization because if one user creates a xml file... I do not want the other user to send this xml file instead of his) and receives results that are displayed into a gridview.

    To be more precise I think it's about multithreading, but I couldn't figure out how to implement it.

    Thank you

    Tuesday, October 11, 2011 6:41 AM
  • User-1856974186 posted

    You can use sessions if the data is per user. ASP.NET supports sessions for anonymous users; just add the following to web.config:

      <anonymousIdentification enabled="true"/>

    You can then use Session instead of Application. You can use Session.SessionID to uniquely identify the user.

    <anonymousIdentification enabled="true"/>
    Tuesday, October 11, 2011 8:13 AM
  • User-1389429259 posted

    So I need to make all the variables work with session? I did that but not for all of them (they are too many), thats why I applied application.lock, because there are hundreds of variables, basically the application is a window forms application that I managed to make it work as a web application. And what about creating, writing and reading from .xml files? How do I protect them from 2 or more requests that try to read/write to them?

    At this moment I can only use 2,3 browsers from my computer to connect to the website I have uploaded the application, with session variables and application.lock it works, I don't get mixed values.... but I don't really know what will happen when 30 users push the submit button at the same time... one by one the application.lock will block the application for just a user at a time... and I think that the last user will probably dye of old age before making it to the last step...

    Maybe I should try with 2 or 3 computers with different internet connexions so that the server would be able to make the difference between sessions

    Thanks again 

    Tuesday, October 11, 2011 3:32 PM
  • User-1856974186 posted

    If the variables related to a single user session use Session, or if their are applicable to the entire application use Application. Or both.

    In any application where data is updated by multiple users you have to make a business decision as to what happens if two people update the same data; does the last write win, or do you fail that last write if the data has changed since it was first read. It's a complex area.

    You've also got to consider your logic, since you've moved from a single user, PC based approach to a centralised single server approach. You just can't use the same logic any more.

    Wednesday, October 12, 2011 4:31 AM
  • User-1389429259 posted

    Thanks a lot.

    However if I may ask, where can I find a small project to figure out how others differentiate variables used by the application for everyone but with results for each users?

    Thursday, October 13, 2011 5:47 AM
  • User-1856974186 posted

    I don't know about a project, but you could create a class that contains properties for each item per user, then store that in the application, using the user name as a key. For example:

    public class UserProperties
    {
      public string Name { get;set; }
      public int Age { get;set; }
      ..
    }

    Then in your application you create an instance of this, set the properties and store it in the application:

    UserProperties props = new UserProperties();
    props.Name = "Dave";
    props.Age = 8;

    Application[User.Identity.Name] = props;

    Then to access it:

    UserProperties props = Application[User.Identity.Name] as UserProperies


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 17, 2011 3:56 AM