How to dispose a Timer?

Options
FullTun
edited October 2012 in Photon Server
I am using a timer (Systems.timers) in my application. Even the class object gets disposed, the timer keeps run. I even tried to dispose the timer. But it won't just get disposed.

Here's the code:

protected virtual void StartTokenTimeOutTimer() {

timer = new Timer();
timer.Interval = 5000;
timer.Elapsed += new ElapsedEventHandler(ProcessTimerEvent);
timer.Start();


}

protected void ProcessTimerEvent(object sender, EventArgs e)
{
if(counter > 0)
Log.Debug("Counter: " + counter);
counter--;

if (counter <= 0) {
Log.Debug("Inside IF");
tokenTimeOut = true;
timer.Dispose();
this.HandleTokenOperation((LitePeer)this.tokenPossessingActor.Peer, lastTokenRequest, lastTokenSendParameters);

}

}

Comments

  • dreamora
    Options
    do not use timers, use the schedule function. That way it will be properly handled.

    Timers create new threads which not only mess stuff up they can also lead to crashes of the whole game server / server due to exceptions.
    As distinct threads they also life and exist outside the room that creates them and are not maintained by the Photon applications automatically. You would need to stop and dispose the timer manually in the room handling code