Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Help Understanding Static Class Behavior on ASP.Net Application

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
P1ST0LPETE
Asked:
P1ST0LPETE
2 Solutions
 
Richard LeeSoftware EnthusiastCommented:
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
 
iHadiCommented:
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
 
NightmanCTOCommented:
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
 
P1ST0LPETEAuthor Commented:
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

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now