タクシー(これはゲームアプリケーションです)に電話をかけることができるシステムがあり、このタクシーには10秒かかります。問題は です。私もcanceltaxi関数を持っていて、System.Threading.Timerを停止する方法を知る必要があります。なぜなら、タクシーを注文すると、キャンセルした後です。 別のタクシーを注文した後、それはまだ古いタクシータイマー、 を使用しているので、10に来るのに2秒かかりますそれを停止するには?System.Threading.Timerを停止する方法
私はこのコードを試しましたが、それでも停止しません。私はそれを取り消したいときにこのvoidと呼んでいます。
public void StopTaxiTimer()
{
taxiTimerInstance.Dispose();
taxiTimerInstance = null;
this.Dispose();
}
全クラス:CallTaxiオン
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);
}
}
}
:
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