views:

26

answers:

3

I need to be able start a timer when my custom control is loaded. However, I need to be able to do this in the abstract class that all controls of this type inherit from.

A: 

Not sure what's your issue is? There are three timers available in .NET and you should be probably able use any of them. Check this article to understand choices and differences between them.

VinayC
+1  A: 

The control does have a Loaded event that you can attach to in your abstract base class in order to start the timer.

// in ctor of abstract control
_timer = new DispatcherTimer(...);
Loaded += (s,e) => _timer.Start();
aqwert
Isn't it true that constructors are not inherited?
Justin
Not inherited but still called when constructed via a derived type
aqwert
Thank you! I wasted quite a bit of time on this simple misunderstanding.
Justin
A: 

Do you mean a control as abstract one. Like below?
Try the following:

public partial class MainWindow : Window
{
    MyControl mycontrol;

    public MainWindow()
    {
        InitializeComponent();

        //// Your logics...

        mycontrol.Dispatcher.BeginInvoke((Action)(() =>
            {
                 /// Try invoking timer here...
            }));
    } 
}

public abstract class MyControl: Control
{

}

HTH

Avatar