Windows 服务在 for 循环内发送请求两次

Windows Service sends request twice within for loop

我正在为我的在线充值网站使用 windows 服务。 我正在从 table 中选择请求的号码并通过 api 发送并获得回复。但有时它会为同一个号码发送两次请求,而我的 table 只包含该号码的一个条目。 我的整页代码如下:

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>DATEADD(minute,-5,GETDATE())", conn);
                DataTable dt = new DataTable();
                adap.Fill(dt);
                for (int i = 0; i < 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)
            {

            }
        } 
    }
}

关于我哪里出错的任何想法?

我认为你的 for 循环本身运行了不止一次,即使你在 table 中有一条记录也是如此。

你开始 i =0 所以放一个负号 (-1)

for (int i = 0; i < dt.Rows.Count - 1; i++)

而不是

for (int i = 0; i < dt.Rows.Count; i++)
protected override void OnStart(string[] args)
        {
            this.timer = new System.Timers.Timer(10000D);
            this.timer.AutoReset = true;
            this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
            this.timer.Start();
        }
        protected override void OnStop()
        {
            this.timer.Stop();
            this.timer = null;
        }

        protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.proccessQue();
        }

我在 onStart 方法中放置了计时器构造函数并从 processQue 方法中删除,现在它工作正常