STOPPT, also habe ich ein System, in dem Sie einstellen können, um ein Taxi anzurufen (dies ist eine Spielanwendung), und dieses Taxi dauert 10 Sekunden, um zu kommen. Das Problem ist Ich habe auch eine Canceltaxi-Funktion und ich muss wissen, wie ich ein System.Threading.Timer stoppen kann, denn wenn sie ein Taxi bestellen, stornieren Sie es nach sagen wir 8 Sekunden dann direkt nachdem sie ein anderes Taxi, das Taxi bestellen dauert 2 Sekunden, um nicht 10 zu kommen, also benutzt es noch den alten Taxitimer, wie höre ich es auf?Wie man einen System.Threading.Timer
Ich habe diesen Code versucht, aber immer noch nicht aufhören .. Ich nenne diese Lücke, wenn ich es abbrechen möchte.
public void StopTaxiTimer()
{
taxiTimerInstance.Dispose();
taxiTimerInstance = null;
this.Dispose();
}
Voll Klasse:
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);
}
}
}
Auf 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);
Try 'taxiTimerInstance.Change (Timeout.Infinite, Timeout.Infinite);' Sie werden dann zurückgesetzt, es auf 10 Sekunden auf die nächste Bestellung muss zurück. https://msdn.microsoft.com/en-us/library/yz1c7148(v=vs.110).aspx – TyCobb