c#如何在构造函数中增加变量- windows服务

本文关键字:变量 windows 服务 增加 构造函数 | 更新日期: 2023-09-27 18:10:01

我有一个Windows服务,我想在每次调用构造函数时更改URL。我在构造函数中通过连接一个变量来形成URL,并且变量正在递增(如下面的代码所示)。

但是,URL没有改变,我仍然从URL下载相同的XML文件。

我已经试过了:

private string Url;
    private string Name = @"D:'test'userko.xml";
    private System.Timers.Timer timer;
    private Downloader xd;
    private static int i;

    public Service1()
    {
        InitializeComponent();
        while (i < 5000)
        {
            Url = "http://tis-toolbox.appspot.com/api/user/id/" + i + ".xml";
            i++;
            if (i == 4999)
                i = 0;
            break;
        }
        xd = new Downloader(Url, Name);
        timer = new System.Timers.Timer();
        timer.Enabled = true;
        timer.Interval = 1000;
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    }
    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("PCTrss reader Service Started.");
        tryDownload();
    }
    protected override void OnStop()
    {
        EventLog.WriteEntry("PCTrss reader Service was shutdown.");
    }
    private void timer_Elapsed(object sender, EventArgs e)
    {
        tryDownload();
    }
    private void tryDownload()
    {
        try
        {
            timer.Stop();
            xd.DownloadXML();
            EventLog.WriteEntry("PCTrss reader downloaded news at " + xd.LastUpdate + ". XML is in " + Name);
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("PCTrss reader Service cannot download feeds " + ex.ToString());
        }
        finally
        {
            timer.Start();
        }
    }
}

这是Downloader类。它从URL读取并保存XML文件。

class Downloader
{
    public DateTime LastUpdate;
    public string Url { get; set; }
    public string Name { get; set; }
    public Downloader(string url, string name) 
    {
        this.Url = url;
        this.Name = name;
    }
    public void DownloadXML()
    {
        if (!Directory.Exists(@"D:'test"))
            Directory.CreateDirectory(@"D:'test");
        LastUpdate = DateTime.Now;

        WebClient client = new WebClient();
        client.Encoding = Encoding.UTF8;
        using( Stream stream = client.OpenRead(Url) )
        using( StreamReader sr = new StreamReader(stream) )
        using( FileStream fs = new FileStream(Name, FileMode.Create, FileAccess.Write))
        using( StreamWriter sw = new StreamWriter(fs) )
        {
            string s = sr.ReadToEnd();
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.Write(s);
        }
    }
}

c#如何在构造函数中增加变量- windows服务

有几个问题:

首先,正如berXpert指出的,while循环中正确的子句应该是i < 5000。其次,如果你想下载5000次

,整个代码应该在while循环中。
while (i < 5000)    
{
    Url = "http://tis-toolbox.appspot.com/api/user/id/" + i + ".xml";
    i++;
    xd = new Downloader(Url, Name);
    timer = new System.Timers.Timer();
    timer.Enabled = true;
    timer.Interval = 1000;
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}

此外,您不需要breakwhile循环中,它不会执行超过5000次(假设i开始为0)

务必将while子句更改为

i < 5000

并将i作为共享静态属性

private static int;