Reputation: 97
I have this timer,
System.Timers.Timer t = new
System.Timers.Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(startAutoSpec);
t.Start();
which is scheduled to attempt to run a function (startAutoSpec) every minute, said function then runs through various codes in attempt to find a live match of League of Legends. So as it is right now it will just keep re-running this function every minute and opening multiple windows of the game which just crashes them all.
So my question is if was possible to access this timer (which is in the Main function) from i.e. the startAutoSpec function and then stop it for the time being before restarting it in another function?
Upvotes: 0
Views: 73
Reputation: 203802
The first parameter passed to the event handler, called the "sender", is the object that fired the event, in this case, the timer. You can cast it to the right type and then stop it.
Upvotes: 0
Reputation: 4395
You need to move the definition of the timer outside of the scope of the Main
function:
System.Timers.Timer t;
void Main()
{
t = new System.Timers.Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(startAutoSpec);
t.Start();
}
void SomeOtherFunction()
{
t.Stop();
}
If you don't understand why this works, google "Variable Scope".
Upvotes: 3