Solved

C# static class methods

Posted on 2006-07-05
1
504 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
[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
1 Comment
 
LVL 25

Accepted Solution

by:
dstanley9 earned 50 total points
ID: 17046737
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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