Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:08:11+00:00 2026-06-18T07:08:11+00:00

I need a way to lock c# threads by user I have my data

  • 0

I need a way to lock c# threads by user

I have my data object and I create new instance for every user.
Every user has several threads that use this object and in I.O. operations I want to lock this object instance for this user only.
Using simple Lock {} is locking all the object instances, there for blocking other user.

I need some simple solution.

Edit

I build new instance of MyDataObj per user;
Then run job that updating some data in MyDataObj every minute;
Using lockObj as lock, lock the data to all the users (Although it’s not static Variables)
I need only to lock the data to the current user

this is the code sample

public sealed class MyDataObj
{
    private static readonly Dictionary<object, MyDataObj> _instances = new Dictionary<object, MyDataObj>();
    public object lockObj = new object();
    public bool jobRunning = false;
    private string data = string.Empty;
    //// --------- constractor -------------------
    private MyDataObj(int key)
    {
        LoadMyDataObj(key);
    }

    public static MyDataObj GetInstance(int key)
    {
        lock (_instances)
        {
            MyDataObj instance;
            if (_instances.TryGetValue(key, out instance))
            {
                instance = _instances[key];
                return instance;
            }
            instance = new MyDataObj(key);
            return instance;
        }
    }

    private void LoadMyDataObj(int key)
    {
        // get the data from db

    }


    public void UpdateMyData(string newData)
    {
        lock (lockObj)
        {
            this.data = newData;
        }
    }
    public string ReadMyData()
    {
        lock (lockObj)
        {
            return this.data;
        }
    }



    public class ActionObject
    {
        MyDataObj myDataObj;
        int UserID;
        //// --------- constractor -------------------
        public ActionObject(int userid)
        {
            this.UserID = userid;
            myDataObj = MyDataObj.GetInstance(userid);
            if (!myDataObj.jobRunning)
            {
                jobs jbs = new jobs(myDataObj);
                System.Threading.Thread RunJob = new System.Threading.Thread(new System.Threading.ThreadStart(jbs.dominutesAssignment));
                RunJob.Start();
                myDataObj.jobRunning = true;
            }
        }
        public ActionObject()
        {
            myDataObj = MyDataObj.GetInstance(this.UserID);
            myDataObj.UpdateMyData("some data");
        }
    }


    public class jobs
    {
        MyDataObj myDataObj = null;

        public jobs(MyDataObj grp)
        {
            this.myDataObj = grp;
        }
        public void dominutesAssignment()
        {
            while (true)
            {
                myDataObj.ReadMyData();
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:08:12+00:00Added an answer on June 18, 2026 at 7:08 am

    I need a way to lock c# threads by user. I have my data object and I create new instance for every user

    Create one lock per user. Or if the user exists longer than the threads: Use the user object as the lock.

    lock (userOrTheUserObject)
    {
        //Do some op
    }
    

    Every user has several threads that use this object and in I.O. operations

    That sounds more like you should use asynchronous IO instead of creating several threads (which will be less effecient)

    I want to lock this object instance for this user only. Using simple Lock {} is locking all the object instances, there for blocking other user.

    If the object is shared between all users you HAVE to lock it using lock. The lock won’t be very effective otherwise. The other object is to redesign the object to now be shared.

    I need some simple solution.

    There are no simple threading solutions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to store application-level data (i.e. cross user sessions) in ASP.NET.
I have an shared object that need to be send to a system API
I'll have a database object that can be accessed from multiple threads as well
Consider the following situation: I have an object foo that is used by multiple
I need to obtain a lock in two different threads in order to access
I have a short lock guarded section in a method (that serves the request
I have android app that talks to server and syncs some data int SQLite
Say I have this function (assume I'm accessing Cache in a threadsafe way): object
I am facing the problem, that I have an C# (.NET) object shared among
I have a counter that's used by multiple threads to write to a specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.