Solved

Help Understanding Static Class Behavior on ASP.Net Application

Posted on 2010-11-24
4
524 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now