?
Solved

Help Understanding Static Class Behavior on ASP.Net Application

Posted on 2010-11-24
4
Medium Priority
?
539 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 1000 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

719 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