user1025125
user1025125

Reputation:

IIS Multithreading

I have a web application under IIS 7. There is a page with Button1. When I click this Button1 the following method fires:

string url = "http://example.com";

string resultStr = "";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

string encoding = resp.CharacterSet;
if (encoding.Equals(String.Empty)) encoding = "UTF-8";

Stream strResponse = resp.GetResponseStream();

int bytesSize = 0, c = 0;
byte[] downBuffer = new byte[2048];//2kb

while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
    if (++c > 100) break;//200kb - max
    downBuffer = Encoding.Convert(Encoding.GetEncoding(encoding), Encoding.UTF8, downBuffer);

    string tempString = Encoding.UTF8.GetString(downBuffer, 0, bytesSize);
    resultStr += tempString;
}

strResponse.Close();

TextBox1.Text = resultStr;

As you can see TextBox1 will contain html code of remote page.

There is a problem: while this method is running I can't load other pages! How to solve this problem?

I know that there is an application pool which store application threads so server can process several threads at the same time... But it doesn't works for me. Why?

Upvotes: 4

Views: 1805

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

I suspect that you are using Session in your application, do you? Session is not thread-safe. This means that if you are using Session, ASP.NET locks the entire request and doesn't allow other requests (from the same Session) to run in parallel. Requests from the same session run sequentially. You can have multiple requests from different sessions running in parallel though.

In fact it's a little more subtle than that. ASP.NET uses a ReaderWriterLock to synchronize access to the session object, meaning that you can have 2 requests from the same session that are only reading from it run in parallel, but as long as you have a write access to the session it will block other parallel requests from the same session.

You could control this using the EnableSessionState="ReadOnly" in your page to indicate that it is only reading from the session and thus allow for parallel execution of requests.

Upvotes: 4

Related Questions