I am trying to use the timer class in C# for this TCM Updater and I keep getting an error: “Delegate to an instance method cannot have null ‘this’.”
I thought I would just have to add “this” to the beginning of the function, but still getting that error. Any idea what I might need to do?
this is the code I am using (error on function call 5 lines down):
public Form1()
{
InitializeComponent();
updateTimer = new System.Timers.Timer(10000);
updateTimer.Elapsed += new ElapsedEventHandler(this.onUpdateEvent); <--- Error here
updateTimer.Interval = 2000;
updateTimer.Enabled = true;
}
private void OnUpdateEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
Change this:
updateTimer.Elapsed += new ElapsedEventHandler(this.onUpdateEvent);To this:
updateTimer.Elapsed += new ElapsedEventHandler(OnUpdateEvent);Or the simplified syntax (as others have suggested):
updateTimer.Elapsed += OnUpdateEvent;