2

I am currently converting a Windows Phone 7 application to its web counterpart. The application uses a big main thread from which the data is gathered, and for the moment I have just copied and pasted it as is (just a standard thread operation), in my ASP.NET MVC controller.

Sync _Sync = new Sync();
_Sync.StartSync();

The tasks work OK, but because the thread makes use of global data set from the cookies, issues arise when accessing the page with 2 different usernames. For example, if I login with "user1" in Firefox and then try to login in Chrome with another user (say "user2"), then it will automatically change the async data gathered for the first user; meaning that I will always see data pulled out from the last user logged in (regardless of the fact that I was just logged in in Firefox with another user, initially), and not each others' separate data.

In other words, the thread doesn't start separately for each individual user. How could I fix this behavior?

5
  • Can you personalize sync by user name? Commented May 4, 2012 at 14:54
  • Does the Sync type utilize any static fields or properties for storing the user data? Commented May 4, 2012 at 14:57
  • "the thread makes use of global data set from the cookies", this doesn't sound good to me, perhaps when you create your Sync object, you can assign it a value that uniquely ties it to a specific user instead of using globals. Commented May 4, 2012 at 14:58
  • 1
    @ChrisShouts Yes, all the global fields needed for the sync are retained in a static object. Should I create a new instance of it every time? Commented May 4, 2012 at 15:02
  • Unfortunately, I can't personalize it in this way. Commented May 4, 2012 at 15:03

1 Answer 1

3

Static fields and properties are shared across threads and should generally not be used to store data that pertains to a specific user or web request.

One of the following suggestions should fix your threading issues and keep your user data separate.

  • Create an instance of the Sync class for each request and remove any static fields / properties from the class.
  • Add a method to the Sync class that returns an instance of the data for a specific user instead of storing the data in static fields / properties.
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, changing the static fields solved the problem. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.