title: Optimizing Windows Service for sending HTTP requests
body: <p>The application that I'm writing sends SMS messages that are stored in databases. For this portion, I'm writing a windows service that will go through a selection of databases and then send the waiting messages as HTTP requests.</p>
  
  <p>Because the application is sending SMS, speed is important. Right now, I'm only getting about 15 requests sent per second. Right now, the application creates <code>SMSMessages</code> and then puts them in a synchronized queue. I use multithreading to run 20 threads at a time from that queue. </p>
  
  <p>I noticed that if I ran too many threads then the application actually slowed down the number of messages sent per second. </p>
  
  <p>What I'm trying to figure out is if there is a faster way than what I'm doing to send the requests. Is there a better way to organize my threads, or should I use threadpooling or asynchronous requests to optimize the application?</p>
  
  <p>The main code is here:</p>
  
  <pre><code>            Queue Messages = new Queue();
              DataRow[] Rows = dtSMSCombined.Select(); //Created from a datatable
              foreach (DataRow Row in Rows)
              {
                  ... //Get information from the row.
  
                  SMSMessage oSMS = new SMSMessage(Keyword, Number, Message, MessageID);
                  Messages.Enqueue(oSMS);
              }
  
              Queue SyncedMessages = Queue.Synchronized(Messages);
              var tasks = new Task[20];
  
              for (int i = 0; i &lt; 20; i++)
              {
                  tasks[i] = Task.Factory.StartNew(() =&gt;
                      { //each thread will pull out new items from the queue as they finish
                          while (SyncedMessages.Count &gt; 0)
                          {
                              Response = new XDocument();
                              SMSMessage oSMS = (SMSMessage)SyncedMessages.Dequeue();
  
                              if (oSMS.GetMessage() != null &amp;&amp; oSMS.GetMessage() != string.Empty)
                              {
                                  Response = oSMS.SendSMS();
                              }
                              string ResponseCode = (string)Response.Descendants("response").First();
                              if (ResponseCode == "ok")
                              {
                                  oSMS.sResponseCode = ResponseCode;
                                  oSMS.dCompleted = DateTime.Now;
                              }
                              else { }
  
                              oSMS.DTInsert();
                          }
                      });
              }
  
              while (tasks.Any(t =&gt; !t.IsCompleted)) { }
  </code></pre>
  
  <p>Here is the <code>SendSMS()</code> method from the <code>SMSMessage</code> class:</p>
  
  <pre><code>    public XDocument SendSMS()
      {
          XML = "&lt;message id=\""+ lMessageID +"\"&gt;&lt;partnerpassword&gt;" + PartnerPassword + "&lt;/partnerpassword&gt;&lt;content&gt;" + sMessage + "&lt;/content&gt;&lt;/message&gt;";
          URL = "http://sloocetech.net:****/spi-war/spi/" + PartnerID + "/" + sRecipient + "/" + Keyword + "/messages/mt";
          HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
          Request.Proxy = null;
  
          RequestBytes = System.Text.Encoding.ASCII.GetBytes(XML);
          Request.Method = "POST";
          Request.ContentType = "text/xml;charset=utf-8";
          Request.ContentLength = RequestBytes.Length;
          RequestStream = Request.GetRequestStream();
          RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
          RequestStream.Close();
  
          HttpWebResponse Resp = (HttpWebResponse)Request.GetResponse();
          oReader = new StreamReader(Resp.GetResponseStream(), System.Text.Encoding.Default);
          string backstr = oReader.ReadToEnd();
  
          oReader.Close();
          Resp.Close();
  
          Doc = XDocument.Parse(backstr);
          return Doc;
      } 
  </code></pre>