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

本文关键字:请求 两次 服务 for 循环 Windows | 更新日期: 2023-09-27 17:56:23

我正在为我的在线充值网站使用Windows服务。我正在从表中选择请求的数字并通过 api 发送并获得响应。但有时它的发送请求两次用于相同的数字,而我的表只包含一个该数字的条目。我的整页代码如下:

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)
            {
            }
        } 
    }
}

关于我哪里出错了有什么想法吗?

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

我认为您的 for 循环即使您在表中有一条记录,它也会自行运行多次。

你开始 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方法中删除,现在它工作正常