Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:19:24+00:00 2026-05-29T05:19:24+00:00

I am writing a service which requires multiple timers (system) to be created. The

  • 0

I am writing a service which requires multiple timers (system) to be created. The number of timers to create is not known until runtime, it’s based upon the contents on an external (XML) config file.
Each timer will be assigned a different task and a different interval.

How can I create x number of timers dynamically? I want to use a count from the for loop in the class instance name, but I don’t seem to be able to figure out how to do this.

Current code as below which creates one timer with the last result of the for loop. I want to create a timer for each loop.

I am learning C# so examples would be very helpful.

var items = GetDownloadList();
int CountDownloadListItems = DownloadItemsCount();

for (int i = 0; i < CountDownloadListItems; i++)
{
    if (ClassLib.UrlExists.Check(items[i, 2], items[i, 0]))
    {
        //new instance of timers

        Timers downloadTimer = new Timers();
        downloadTimer.CreateTimer(items[i, 2], items[i, 3], items[i, 4], items[i, 0], items[i, 5], items[i, 6]);

    }
}

// My Timers Class

public class Timers
{

    //timer
    private Timer _timer = new Timer();
    private volatile bool _requestStop = false;

    public Timers()
    {
        _timer.Interval = 20;
        _timer.Elapsed += Timed;
        _timer.AutoReset = false;

    }

    private string _Url;
    private string _Save;
    private string _File;
    private string _Id;
    private string _FreqValue;
    private string _FreqUnit;

    public string Url
    {
        get { return _Url; }
        set { _Url = value; }
    }

    public string Save
    {
        get { return _Save; }
        set { _Save = value; }
    }

    public string File
    {
        get { return _File; }
        set { _File = value; }
    }

    public string Id
    {
        get { return _Id; }
        set { _Id = value; }
    }

    public string FreqValue
    {
        get { return _FreqValue; }
        set { _FreqValue = value; }
    }

    public string FreqUnit
    {
        get { return _FreqUnit; }
        set { _FreqUnit = value; }
    }

    public void CreateTimer(string url, string save,string file,string id,string freqValue,string freqUnit)
    {
        this.Url = url;
        this.Save = save;
        this.File = file;
        this.Id = id;
        this.FreqValue = FreqValue;
        this.FreqUnit = FreqUnit;

        _timer.Interval = 2000;
        _timer.Start();
    }

    private void Timed(object source, ElapsedEventArgs e)
    {
        _timer.Stop();

        //StreamWriter sr = new StreamWriter("E:\\downloader\\"+Id+".txt");
        //sr.Write(Url+" "+Save+" "+File+"\n\n");
        //sr.Close();

        ClassLib.Update.File(Url,Save,File,Id);

        _timer.Start();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:19:24+00:00Added an answer on May 29, 2026 at 5:19 am

    You do appear to be a creating a new Timers for each value in the for loop. However, you don’t appear to keeping a reference to them.

    How about adding a Dictionary<string, Timers> variable that stores the references alonsgide the ‘name’?

     var timersDict = new Dictionary<string, Timers>();
     var items = GetDownloadList();
     int CountDownloadListItems = DownloadItemsCount();
    
    
    for (int i = 0; i < CountDownloadListItems; i++)
                    {
                        if (ClassLib.UrlExists.Check(items[i, 2], items[i, 0]))
                        {
                            //new instance of timers
    
                            Timers downloadTimer = new Timers();
                            downloadTimer.CreateTimer(items[i, 2], items[i, 3], items[i, 4], items[i, 0], items[i, 5], items[i, 6]);
                            timersDict.Add("Timer" + i, downloadTimer);
    
                        }
    
                    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing WCF service that uses wsHttpBinding binding, which is not hosted in
I'm writing a simple music player, and I've created a playback service which implements
I'm writing a windows service which will be used to monitor some other services
I'm writing a web service which expects one of the parameters (called hlink) to
I've been writing a background service which enforces my client's security policies on company
I'm writing a C application which creates a Windows service. I'd like to check
I am writing a DLL which may run in the context of a service
I am developing an app which requires interaction with a SOAP web service, to
I am writing a Java application that requires access to a database, which is
I'm writing my own ContentProvider which will be synced to a web service using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.