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 4560180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:53:40+00:00 2026-05-21T17:53:40+00:00

I have cleaned and shortened a snippet of my code for a multiuser game

  • 0

I have cleaned and shortened a snippet of my code for a multiuser game to show what I would like to accomplish. So here it is:

    public class Subject {
        public List<IObject> Objects = new List<IObject>();
    }

    public interface IOpenable
    {
        void Open(Subject by, params string[] p);
        void Close(Subject by, params string[] p);
        bool IsOpen { get; }
    }

    public interface IObject {
        void Pickup(Subject by, params string[] p);
        void Drop(Subject by, params string[] p);
    }

    public class Box : IObject, IOpenable
    {
        bool opened = false;
        Subject owner = null;

        public void Pickup(Subject subject, params string[] p) {
            subject.Objects.Add(this);
            this.owner = subject;
        }

        public void Drop(Subject subject, params string[] p)
        {
            subject.Objects.Remove(this);
            this.owner = null;
        }

        public void Open(Subject by, params string[] p)
        {
            this.opened = true;
        }

        public void Close(Subject by, params string[] p)
        {
            this.opened = false;
        }

        public bool IsOpen { get { return opened; } }
    }

What I would like to know is:
How to prevent some user (executing code from another thread) to open a Box that is currently being Picked up by some other user.
I’ve thought of some ways but I think that people here often come up with clever ideas, that could make me avoid some stupid design issue.

EDIT: As suggested in the answers, to use the lock keyword in the open method: this is not exactly what I want, I’ll try to explain with what is allowed and what not:

The network requests we get as input are somehow async and get out of order if issued fast.

  • (1) User 1 issues command PICKUP BOX
  • (2) User 1 issues command OPEN BOX
  • (3) User 1 issues command CLOSE BOX
  • (4) User 2 issues command OPEN BOX
  • (5) User 2 issues command LOOK BOX
  • (6) User 1 issues command OPEN BOX

We get this order:

2,3,1,5,4,6

2 - allow
3 - allow
1 - allow [remains in execution and has not set the owner]
5(comes in between 1) - allow
4(comes in between 1) - disallow (not because already open but because 1 is in execution)
6(comes in between 1) - allow since it is from user 1, and he is currently picking it up

Thanks!

  • 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-05-21T17:53:40+00:00Added an answer on May 21, 2026 at 5:53 pm

    My solution to this is:

    Monitor.TryEnter(obj)
    

    Similar to lock but not blocking.

    So we lock an object when a user start picking it up,

    and when Opening the box do a check that says:

    if (this.subject == subject || Monitor.TryEnter(obj))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have cleaned up my site and discarded lots of pages. I have now
I'm having a breakpoint falsely getting hit. I have cleaned and rebuilt, deleted my
Let's say I have cleaner like this .cleaner:after { content: '.'; display: block; clear:
Many articles (e.g. msdn ) have said that a circular reference cannot be cleaned
I have some data that needs to be cleaned before inserting in to a
I have files that I need cleaned up during the build process. There is
I have a dictionary of objects that need to be cleaned up before they
I have an abstract functor class that overloads operator() and derived objects that implement
What resources have to be manually cleaned up in C# and what are the
The Problem: I have a forms project which instantiates a class that is defined

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.