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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:44:08+00:00 2026-06-09T01:44:08+00:00

In my MVC 3 C# application I have some static object that I want

  • 0

In my MVC 3 C# application I have some static object that I want to be available to one request at a time. Access to it is only through methods, but I want the lock to be kept in between calling its methods.

Calls will be done only in a controller, usually there will be one or two locked blocks of code.

At first I wanted to expose some static public object and use it simply like

lock(MyClass.lockObject)
{
 MyClass.doStuff();
 MyClass.doStuff2();
}

, but I find it error prone, as I might forget to lock it somewhere. I wonder if it is a proper way to use Monitor.Enter() in a constructor, and Monitor.Exit() in a Dispose method, and then change my methods to nonstatic? Say, something like:

public class MyClass:IDisposable
{
    static protected object _locker = new object();
    protected bool isDisposed = false;

    public MyClass()
    {
        Monitor.Enter(_locker);
    }

    public void Dispose()
    {
        if (!isDisposed)
        {
            Monitor.Exit(_locker);
            GC.SuppressFinalize(this);
            isDisposed = true;
        }
    }

    ~SPInstances()
    {
        Dispose();
    }

    public void doStuff()
    {
        if(isDisposed) throw new ObjectDisposedException();
        // do stuff here, etc.
    }
}

Then I could use it as:

using(MyClass myinstance = new MyClass())
{
    myInstance.doStuff();
    myInstance.doStuff2();
}

Then, even if I forgot to wrap code in using, it will still lock and there will be some chance that it will get unlocked during garbage collecting…

I’m not proficient in C# and sometimes overlook some aspects, and threading is never easy to debug later on, so I’m wondering if I’m on a good track. Is it a proper way to achieve my goal?

EDIT:

Extending Master Morality idea, would this way be better (I simplified it a bit, as I need only one instance of resource)?

public class MyClass
{
    static protected readonly MyResourceType _myResourceStatic = new MyResourceType();
    static public void DoWork(Action<MyClass> action)
    {
        lock(_myResource)
        {
            action(new MyClass(_myResource));
        }        
    }

    protected MyClass(MyResourceType myResource)
    {
        _myResource = myResource;
    }
    protected readonly _myResource;

    public void DoFirstThing() { ... }
    public void DoSecondThing(){ ... }
}

MyClass.DoWork(x => 
{
    x.DoFirstThing();
    // do unrelated stuff
    x.DoSecondThing();
});
  • 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-09T01:44:10+00:00Added an answer on June 9, 2026 at 1:44 am

    IMHO it’s better to lock inside your own methods. That way, another programmer, or yourself later, doesn’t have to remember to lock before they call a method, and it’s simple enough.

    public class MyClass
    {
        private static readonly object _gate = new object();
    
        /* something that can only be accessed by one thread at a time...*/
        private static MyResourceType MyResource = new MyResourceType();
    
        public void DoSomething()
        {
             lock(_gate)
             {
                /* do something with MyResource, just make sure you
                   DO NOT call another method that locks the gate
                   i.e. this.DoSomethingElse(), in those situations,
                   you can take the logic from DoSomethingElse() and
                   toss it in a private method i.e. _DoSomethingElse().
                 */
             }
        }
    
        private void _DoSomethingElse()
        {
            /* do something else */
        }
    
        public void DoSomethingElse()
        {
             lock(_gate)
             {
                 _DoSomethingElse();
             }
         }
    }
    

    Later that day…

    var myClass = new MyClass();
    myClass.DoSomething();
    

    if you want to be able to call multiple methods with a lock, you can do it with a lambda,
    and to be really safe, wrap it in a helper class.

    public class MyClass
    {
        public MyResourceType MyResource { get; set; }
        public void DoFirstThing() { ... }
        public void DoSecondThing(){ ... }
    }
    
    public class MyClassHelper
    {
        private static readonly object _gate = new Object();
        private static MyResourceType MyResource = new MyResourceType();
    
        private MyClass _myClass = new MyClass();        
    
        public void DoWork(Action<MyClass> action)
        {
             lock(_gate)
             {
                 _myClass.MyResource = MyResource;
                 action(_myClass);
                 _myClass.MyResource = null;
             }
        }
    }
    
    ...
    
    var myClassHelper = new MyClassHelper();
    myClassHelper.DoWork(x => 
        {
            x.DoFirstThing();
            x.DoSecondThing();
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some static (pure html) pages in my MVC application that I need
I have an ASP.NET MVC application, with some RESTful services that I'm trying to
I have a ASP.NET MVC application for which I want to write some stress
I am using sping3 mvc in my application. I have some entities which need
I have a asp.net mvc application. Some pages require SSL, is there a way
I have 5 views in my ASP.NET MVC application. Some data were captured from
In my ASP .NET MVC application i have a link that refreshes the preview
I have one question; In my ASP.NET MVC web application have to do certain
I have a spring mvc application that I have broken up into separate maven
I'm developing an MVC application and I have a routine that gets the currently

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.