Solved

C# static class methods

Posted on 2006-07-05
1
494 Views
Last Modified: 2007-10-18
I have the following method in a static class in c#. Many users may potentially access this at the same time, each passing their own values and should get their own return value. On a small scale test this seems to work fine. My question is regarding a concern that if a procedure is static, it should technically only be able to exist in one place and thus either multiple requests to the method would que up in awaiting access if is currently being accessed by another user or worse (but apparently not probable) that the wrong user would recieve a result from another user.

Can anyone clarify this? My inclination is to move such procedures to a standard class.

            private static string HTTPGetRequest(string sURLQS)
            {
                  string tempString = null;
                  int    count      = 0;
                  StringBuilder sb  = new StringBuilder();

                  // used on each read operation
                  byte[]        buf = new byte[8192];

                  // prepare the web page we will be asking for
                  HttpWebRequest  request  = (HttpWebRequest)
                        WebRequest.Create(sURLQS);

                  // execute the request
                  HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                  // we will read data via the response stream
                  Stream resStream = response.GetResponseStream();

                  do
                  {
                        // fill the buffer with data
                        count = resStream.Read(buf, 0, buf.Length);

                        // make sure we read some data
                        if (count != 0)
                        {
                              // translate from bytes to ASCII text
                              tempString = Encoding.ASCII.GetString(buf, 0, count);

                              // continue building the string
                              sb.Append(tempString);
                        }
                  }
                  while (count > 0); // any more data to read?

                  // print out page source
                  return sb.ToString();
            }
0
Comment
Question by:dnkahn
1 Comment
 
LVL 25

Accepted Solution

by:
dstanley9 earned 50 total points
Comment Utility
No, static _methods_ are OK, it's static _variables_ you need to be careful of in multithreqading environments. The static methods will create local varaibles in the context of the running thread, so there are no issues with synchronization.  I don't see you using and members, only local variables, so you're OK.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

8 Experts available now in Live!

Get 1:1 Help Now