所以我有一個系統,您可以設置打電話給出租車(這是一個遊戲應用程序),而這輛出租車需要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