1

In a WinForms (C#) application I have several custom controls created at run-time. These custom controls are called JobTimers and inherit/extend the standard control Timer.

public class JobTimer : System.Windows.Forms.Timer
{
    private int IntJobID;
    public int JobID
    {
        get{return IntJobID;}
        set{IntJobID = value;}
    }
}

public static void CreateTimer(int JobID) 
{
    JobTimer ControlJobTimer = new JobTimer();
    ControlJobTimer.Enabled = true;
    ControlJobTimer.JobID = JobID;
    ControlJobTimer.Interval = 30000;
    ControlJobTimer.Tick += new EventHandler(JobTimer_Tick);
    ControlJobTimer.Start(); 
}

Similar to how we would query the collection of standard or custom controls on the Form - Can we access/query the collection of these custom JobTimer controls?

NOTE: The reason for this question to appear is that I don't see these controls getting docked/placed in any of the forms, thereby existing purely in the program's memory only. Also upon stopping/exiting the application, these controls are gone.

In other words, how does one get a list of all timers in an application along with access to their properties?

Ralt
  • 2,084
  • 1
  • 26
  • 38
prgSRR
  • 169
  • 4
  • 11
  • You get a list of timers by creating a `List`. How's that for kicking in an opened door ;) What you have now doesn't give you any decent way to get a reference back to the timer object. You had one but it is just a local variable that's gonzo after CreateTimer() returns. All that's left is the *sender* argument in the Tick event handler. Use a List. – Hans Passant Sep 11 '14 at 11:33

3 Answers3

0
    public static List<JobTimer> _timers;

    public class JobTimer : System.Windows.Forms.Timer
    {
        private int IntJobID;
        public int JobID
        {
            get { return IntJobID; }
            set { IntJobID = value; }
        }
    }

    public static void CreateTimer(int JobID)
    {
        if (_timers == null)
        {
            _timers = new List<JobTimer>();
        }
        JobTimer ControlJobTimer = new JobTimer();
        ControlJobTimer.Enabled = true;
        ControlJobTimer.JobID = JobID;
        ControlJobTimer.Interval = 30000;
        ControlJobTimer.Tick += new EventHandler(JobTimer_Tick);
        ControlJobTimer.Start();
        _timers.Add(ControlJobTimer);
    }

then you can access the global list _timers, all created instances of jobtimer will be added there

user1519979
  • 1,854
  • 15
  • 26
  • Thanks for the solution. Could you shed some light on this part: if (_timers == null) { _timers = new List(); } – prgSRR Sep 11 '14 at 11:09
  • it's just the creation of the instance of the list, if _timers is not assigned then you have to create a new list, else it's already created and nothing is done. you can do it at the start of the programm as well, then you do not need the if statement in CreateTimer. E.g. you can create the instance in the form_load event (http://msdn.microsoft.com/en-us//library/system.windows.forms.form.load(v=vs.110).aspx) -- > _timers = new List(); – user1519979 Sep 11 '14 at 11:17
  • @user1519979 just curious why you wouldn't instantiate the list when you declare it? – Jeremy Thompson Sep 12 '14 at 03:31
0

This code smells. Derived custom Controls from Timers is a really expensive way to achieve what you want. You could use threading, background workers, subscription/publishing models instead that would be much more optimal.

Can we access/query the collection of these custom JobTimer controls?

Simple, when you declare the JobTimers keep access to them in the parent form, eg:

List<JobTimer> jobTimers = new List<JobTimer>();
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks for the solution. I'd start using threading, background workers, etc. when I become proficient in those areas... :) – prgSRR Sep 11 '14 at 11:10
  • Ok, promise me you will make an effort to try a `background worker`example. I'll give +5 points now so you can vote on [so] in expectation that in return you will give better solutions a go. – Jeremy Thompson Sep 11 '14 at 12:23
  • You bet..!!! :) I'm reading up on background workers already and will be sure to employ them in upcoming projects. Thanks for the guidance. – prgSRR Sep 11 '14 at 12:40
0

As others already stated, you have to save and keep safe created timers yourself. There is no AllTimers collection, so you have to create and manage one.

Subclassing Timer is bad idea. Here is probably more proper and easily extendable version:

using System.Windows.Forms;

public class JobTimer
{
    public Timer Timer {get; set;}
    public string ID {get; set;}
}

public List<JobTimer> _timers = new List<JobTimer>();

public static void CreateTimer(int id) 
{
    var timer = new Timer() { Interval = 30000 };
    var job = new JobTimer() { Timer = timer, ID = id };
    _timers.Add(job);
    timer.Tick += JobTimer_Tick;
    timer.Start(); 
}

public static void DisposeTimers()
{
    foreach(var timer in _timers)
        timer.Dispose();
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Thanks for the solution. Creating inherited class was the only way known to me for adding custom properties. I'll give this a go as well. – prgSRR Sep 11 '14 at 12:47