ThreadBotClient.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using CommonLang;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace XmdsBattleClientBot.Bot
  7. {
  8. public class ThreadBotClient : BotClient
  9. {
  10. private System.Threading.Timer update_timer;
  11. private long last_update_time;
  12. public override bool IsRunning { get { return update_timer != null; } }
  13. public override string RoleName { get { return base.RoleName; } }
  14. public ThreadBotClient(string user, string pswd, BotConfig cfg)
  15. : base(user, pswd, cfg, null)
  16. {
  17. }
  18. public override void Start()
  19. {
  20. lock (this)
  21. {
  22. if (update_timer == null)
  23. {
  24. base.Start();
  25. this.last_update_time = CUtils.CurrentTimeMS;
  26. this.update_timer = new System.Threading.Timer(do_update, this,
  27. 1000 / Config.ClientFPS,
  28. 1000 / Config.ClientFPS);
  29. }
  30. }
  31. }
  32. public override void Stop()
  33. {
  34. lock (this)
  35. {
  36. if (update_timer != null)
  37. {
  38. base.Stop();
  39. update_timer.Dispose();
  40. update_timer = null;
  41. }
  42. }
  43. }
  44. private void do_update(object state)
  45. {
  46. lock (this)
  47. {
  48. long curTime = CommonLang.CUtils.CurrentTimeMS;
  49. if (last_update_time == 0)
  50. {
  51. last_update_time = curTime;
  52. }
  53. int intervalMS = (int)(curTime - last_update_time);
  54. last_update_time = curTime;
  55. this.Update(intervalMS);
  56. }
  57. }
  58. public override void Update(int intervalMS)
  59. {
  60. lock (this)
  61. {
  62. base.Update(intervalMS);
  63. }
  64. }
  65. }
  66. }