所以我有一个系统,您可以设置打电话给出租车(这是一个游戏应用程序),而这辆出租车需要10秒钟才能到达。问题是 我也有一个canceltaxi函数,我需要知道如何停止System.Threading.Timer,因为当他们点了一辆出租车后,取消它后 可以说8秒,然后他们点了另一辆出租车后,那辆出租车需要2秒钟来不到10所以它仍然使用旧的计时器, 我该如何阻止它?如何停止System.Threading.Timer
我已经试过这段代码,但它仍然没有停止..当我想取消它时,我称之为无效。
public void StopTaxiTimer()
{
taxiTimerInstance.Dispose();
taxiTimerInstance = null;
this.Dispose();
}
满级:
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);
}
}
}
在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);
尝试'taxiTimerInstance.Change(Timeout.Infinite,Timeout.Infinite);'然后,您将不得不重新回至10秒的下一个数量级上。 https://msdn.microsoft.com/en-us/library/yz1c7148(v=vs.110).aspx – TyCobb