?
Solved

C# static class methods

Posted on 2006-07-05
1
Medium Priority
?
508 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

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