Solved

Help Understanding Static Class Behavior on ASP.Net Application

Posted on 2010-11-24
4
538 Views
Last Modified: 2012-05-10
Experts,

As part of my overall web solution, I have a windows service application that will be running on the web server.  My web application has the ability to communicate with this service.  In my web app I have a static class that I am using to communicate with the service, which includes methods that look like this:
 
public static Boolean SendMessage(String data)
{
    bool messageSent = false;
    for (int i = 0; i < 5; i++)
    {
        if (!connected)
        {
            OpenConnection();
            byte[] bytes = Encoding.ASCII.GetBytes(data);
            netStream.Write(bytes, 0, bytes.Length);
            CloseConnection();
            messageSent = true;
            break;
        }
        else
        {
            System.Threading.Thread.Sleep(5000);
        }
    }
    return messageSent;
}

Open in new window

So my question is: If one web user's session causes the "System.Threading.Thread.Sleep(5000)" line to be hit, how would this effect all other web sessions trying to access/use this static method at the same time?
0
Comment
Question by:P1ST0LPETE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 250 total points
ID: 34208694
Each request within an ASP.NET web application/website is handled on a different thread therefore one user calling the static method SendMessage will not affect another user. Saying that, if there is a shared resource then  data corruption, timeouts, application faults, etc could happen if access to the resource is not properly managed.

One thing to note however is that ASP.NET requests are run on threads from the ThreadPool. The ThreadPool has a limited number of threads available, which for high availability sites, begs that each request finish as quickly as possible. As you can guess if many requests are made and each thread is sleeping for 5 seconds then you may be danger of running out of threads from the ThreadPool. IIS will then issue the message "Service Unavailable" to incoming requests.

DaTribe
0
 
LVL 13

Expert Comment

by:iHadi
ID: 34208709
A static method or field can be accessed by more than one thread, so more than one user can call this method at the same time (each has his own thread).

However the Thread.Sleep() method blocks the thread that called the method only. So a Thread.Sleep for a session does not affect another session, although sharing the connection might, especially if a thread opened a shared connection and while the other thread (session) is using it, the first thread closed it.
0
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 250 total points
ID: 34208833
In this particular case, Thread.Sleep will affect the current thread only - i.e. the thread of the user process that called this method. Also, since it occurs in a section of the code that doesn't appear to access any shared variables it should be safe.

Effectively, each thread that calls a method gets it's very own call stack, so they will remain largely unaffected by it.

There are other possible implications, though:
1. You will have a limited number of available threads in the thread pool on your web service, so numerous concurrent calls to this method will simply result in all other requests waiting until there is an available thread.

2. I assume that "connected" is a static bool that is set to true in the OpenConnection and false in CloseConnection, and that you don't want more than one connection at a time? There is no lock mechanism around the critical section of code - relying on the bool is dangerous, since you run the risk of another process setting that to true in the time between a thread checking it and entering the next line. This means that you can technically have more than one thread executing in the inner code block. To make this thread safe, you would simply do this:

declare in the static class:
private static object syncRoot = new object();

and in your method
        if (!connected)
        {
           lock(syncRoot)
           {
              OpenConnection();
              byte[] bytes = Encoding.ASCII.GetBytes(data);
              netStream.Write(bytes, 0, bytes.Length);
              CloseConnection();
              messageSent = true;
              break;
           }
        }
        else
        {
            System.Threading.Thread.Sleep(5000);
        }

Open in new window


Hope this helps
0
 
LVL 10

Author Closing Comment

by:P1ST0LPETE
ID: 34209363
Thanks for the insight guys.  The web app will not be heavily hit as it is being installed/used on multiple intranets, with the average total user count being between 5-25; and in a typical environment this website should only see about 3-5 concurrent users at a time.  So I don't think I should have too much of a problem with the ThreadPool being maxed out, but that was good info that I had previously no knowledge about.

Also, Nightman, you hit the nail directly on the head.  "connected" is a static boolean variable set to true/false in OpenConnection() and CloseConnection(), that I was attempting to use to prevent multiple users connections to the windows service at the same time.  So that was a great tip about using "lock" to prevent the possibility of two users connecting at the same time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question