2

using asp.net mvc 4,

I created a class with a static method like this

public  class StaticClass
{
    public static int val { get; set; }

    public static string ReturnValueBasedOnInput(int n)
    {
        string res;
        switch (n)
        {
            case 101:
                Thread.Sleep(30000);
                res = "Long lasting response: 101" + val;
                break;
            default:
                res = n.ToString() + " was provided..." + val;
                break;               
        }

        return res;
    }        
}

it is called from my controller :

    public ActionResult Index(int id = 1)
    {            
        ViewBag.returnValue = StaticClass.ReturnValueBasedOnInput(id);
        return View(id);
    }

I expected that when i call the method with a parameter value of 101 the application should be blocked for 30 secs, but it remains responsive. I thought since this is a static method it should be blocked for 30 second for all incoming method calls. can someone explain what happens here?

5
  • 1
    Are you getting the expected return value Long lasting response: 101? Commented Oct 9, 2013 at 21:19
  • Yes, I created a test case and everything is working like this is not a static method, eg. when I call with 101 and start to provide different numbers in a new session those numbers are shown until 30 secs is finished, then 101 is shown with the next number provided Commented Oct 9, 2013 at 21:24
  • How are you calling Index? Is it an AJAX call or a page call? Commented Oct 9, 2013 at 21:25
  • Are you saying it isn't blocking at all or is only blocking for the request that passed 101? Just because the class and method is static, it doesn't mean only 1 thread can access at a time. It means all threads will share the memory and contents of the object. Only the thread that called the method with '101' should block while the others traverse happily. If you want 1 thread to access, you need to use locking. Commented Oct 9, 2013 at 21:29
  • It is working just as you said, so can you please answer my question in the comments of answer 1? Commented Oct 9, 2013 at 21:36

1 Answer 1

1

The thread handling the request to the Index action with id=101 of your controller should be blocked. Threads handling other requests of other sessions will not be blocked. Even other requests for the same session may not be blocked depending on ReadOnly session attribute of corresponding controllers [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)].

Sign up to request clarification or add additional context in comments.

3 Comments

so is it a good idea to use a static method for retrieving data from database in the service layer, for reporting for example?
It is a normal idea, you have to remember that several threads may execute this method at the same time, therefore you should not be modifying any of the static class fields (such as val in your example), or do it using locking.
Thank you very much Igor, I got my answer, since I read everywhere that generally it is not a good idea to use static methods in threaded applications

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.