Search code examples
c#multithreadingtimer

How to STOP a System.Threading.Timer


so I have a system where you can set to call a taxi (this is a game application), and this taxi takes 10 seconds to come. The issue is I also have a canceltaxi function and I need to know how I can stop a System.Threading.Timer because when they order a taxi, cancel it after lets say 8 seconds then straight after they order another taxi, that taxi takes 2 seconds to come not 10 so it still uses the old taxi timer, how do I stop it?

I have tried this code but still it doesnt stop.. I call this void when I want to cancel it.

public void StopTaxiTimer()
        {
            taxiTimerInstance.Dispose();
            taxiTimerInstance = null;
            this.Dispose();
        }

Full class:

using log4net;
using Plus.Communication.Packets.Outgoing.Rooms.Chat;
using Plus.HabboHotel.GameClients;
using Plus.HabboHotel.Roleplay.Instance;
using Plus.HabboHotel.Rooms;
using System;
using System.Threading;

namespace Plus.HabboHotel.Roleplay.Timers
{
    public sealed class TaxiTimer : IDisposable
    {
        private static readonly ILog myLogger = LogManager.GetLogger("Plus.HabboHotel.Roleplay.Timers.DeathTimer");

        private Timer taxiTimerInstance;
        private uint timerTimeSeconds;
        private RoleplayInstance roleplayInstance;

        public TaxiTimer(RoleplayInstance roleplayInstance)
        {
            Console.WriteLine("Setup TaxiTimer for " + roleplayInstance.GetSession().GetHabbo().Username + " (" + roleplayInstance.TaxiWaitTimeSeconds + " seconds)");
            this.timerTimeSeconds = roleplayInstance.TaxiWaitTimeSeconds;
            this.roleplayInstance = roleplayInstance;
            this.taxiTimerInstance = new Timer(new TimerCallback(this.OnTimerElapsed), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }

        public void OnTimerElapsed(object Obj)
        {
            try
            {
                if (taxiTimerInstance == null)
                    return;

                if (roleplayInstance == null || !roleplayInstance.CalledTaxi || roleplayInstance.GetSession() == null || roleplayInstance.GetSession().GetHabbo() == null)
                    return;

                GameClient gameSession = roleplayInstance.GetSession();

                if (roleplayInstance.TaxiWaitTimeSeconds < 1)
                {
                    Room currentRoom = gameSession.GetHabbo().CurrentRoom;
                    if (currentRoom == null)
                        return;

                    RoomUser roomUser = currentRoom.GetRoomUserManager().GetRoomUserByHabbo(gameSession.GetHabbo().Id);
                    if (roomUser == null)
                        return;

                    roleplayInstance.CalledTaxi = false;
                    currentRoom.SendMessage(new ShoutComposer(roomUser.VirtualId, "*Gets transported to my destination*", 0, roomUser.LastBubble));
                    gameSession.GetHabbo().PrepareRoom(roleplayInstance.TaxiRoomId, string.Empty);
                }
                else
                {
                    roleplayInstance.TaxiWaitTimeSeconds--;
                }
            }
            catch (Exception ex)
            {
                myLogger.Error(ex.Message);
                myLogger.Error(ex.StackTrace);
            }
        }

        public void StopTaxiTimer()
        {
            taxiTimerInstance.Dispose();
            taxiTimerInstance = null;
            this.Dispose();
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
    }
}

On CallTaxi:

roleplayInstance.TaxiWaitTimeSeconds = Convert.ToUInt32(PlusEnvironment.GetRPManager().GetSettings().GetSettingValueByKey("roleplay_taxi_wait_seconds"));
                    roleplayInstance.TaxiRoomId = goingTo.RoomId;
                    roleplayInstance.TaxiTimer = new HabboHotel.Roleplay.Timers.TaxiTimer(roleplayInstance);

Solution

  • I need to know how I can stop a System.Threading.Timer because when they order a taxi, cancel it after lets say 8 seconds then straight after they order another taxi, that taxi takes 2 seconds to come not 10 so it still uses the old taxi timer, how do I stop it?

    Take a step back. You don't have to worry about canceling a timer if you never make one.

    You are describing an asynchronous workflow with cancellation. C# and the .NET framework already has this feature, so use it rather than trying to roll your own.

    Make an async workflow method that awaits a Task.Delay task that takes a CancellationToken. The continuation of the delay is the arrival of the taxi; the cancellation method causes the task to fail by canceling the token.

    There is plenty of documentation on this mechanism, so start reading it. A good place to start is here:

    https://msdn.microsoft.com/en-us/library/dd997364