title: Windows Service sends request twice within for loop
body: <p>I'm using a windows service for my online recharge website.
  I'm selecting requested numbers from table and send it through api and get response. But some times its send request twice for same number while my table contains only one entry for that number.
  My full page code is given below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Data.SqlClient;
  using System.Diagnostics;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using System.IO;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Configuration;
  using System.Net;
  
  namespace bulkRechargeService
  {
      public partial class bulkRecharge : ServiceBase
      {
          System.Timers.Timer timer; 
          SqlConnection conn;
  
          public bulkRecharge()
          {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args)
          {
              proccessQue();
          }
          protected override void OnStop()
          {
              timer.Stop();
              timer.Enabled = false;
          }
  
          protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
          {
              proccessQue();
          }
          public void proccessQue()
          {
              try
              {
                  timer = new System.Timers.Timer();
                  timer.Interval = (1000) * 10;
                  timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
                  conn = new SqlConnection(ConfigurationManager.AppSettings["ConnString1"]);
                  SqlDataAdapter adap = new SqlDataAdapter("SELECT * FROM recharge_request WHERE is_done=0 AND rdate&gt;DATEADD(minute,-5,GETDATE())", conn);
                  DataTable dt = new DataTable();
                  adap.Fill(dt);
                  for (int i = 0; i &lt; dt.Rows.Count; i++)
                  {
                      string rId = dt.Rows[i]["id"] + "";
                      string operators = dt.Rows[i]["operator"] + "";
                      string mobileNo = dt.Rows[i]["mobile_no"] + "";
                      string amount = dt.Rows[i]["amount"] + "";
                      string rechargeType = dt.Rows[i]["recharge_type"] + "";
                      string referenceId = dt.Rows[i]["user_reference_id"] + "";
  
                      string api = "http://*******************************************";
  
                      HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(api);
                      HttpWebResponse httpres = (HttpWebResponse)httpreq.GetResponse();
                      StreamReader sr = new StreamReader(httpres.GetResponseStream());
                      string results = sr.ReadToEnd();
                      sr.Close();
  
                      SqlCommand cmd = new SqlCommand("UPDATE recharge_request SET is_done=1,rdate=GETDATE(),udate=GETDATE() WHERE id=" + rId, conn);
                      conn.Open();
                      cmd.ExecuteNonQuery();
                      conn.Close();
                  }
                  timer.Enabled = true;
                  timer.Start();
              }
              catch(Exception ex)
              {
  
              }
          } 
      }
  }
  </code></pre>
  
  <p>Any ideas on where I'm going wrong?</p>