Guest User

Untitled

a guest
Dec 4th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. using YetAnotherRelogger.Helpers;
  5. using YetAnotherRelogger.Helpers.Tools;
  6.  
  7. namespace YetAnotherRelogger
  8. {
  9. public sealed class ForegroundChecker
  10. {
  11. #region singleton
  12. static readonly ForegroundChecker instance = new ForegroundChecker();
  13.  
  14. static ForegroundChecker()
  15. {
  16. }
  17.  
  18. ForegroundChecker()
  19. {
  20. }
  21.  
  22. public static ForegroundChecker Instance
  23. {
  24. get
  25. {
  26. return instance;
  27. }
  28. }
  29. #endregion
  30.  
  31. private Thread _fcThread;
  32.  
  33. public void Start()
  34. {
  35. if (_fcThread != null)
  36. _fcThread.Abort();
  37.  
  38. _fcThread = new Thread(new ThreadStart(ForegroundCheckerWorker)) {IsBackground = true};
  39. _fcThread.Start();
  40. }
  41. public void Stop()
  42. {
  43. _fcThread.Abort();
  44. }
  45.  
  46. private IntPtr _lastDiablo;
  47. private IntPtr _lastDemonbuddy;
  48. private void ForegroundCheckerWorker()
  49. {
  50. try
  51. {
  52. while (true)
  53. {
  54. var bots = BotSettings.Instance.Bots;
  55. var hwnd = WinAPI.GetForegroundWindow();
  56.  
  57. if (_lastDiablo != hwnd)
  58. {
  59. _lastDemonbuddy = _lastDiablo = IntPtr.Zero;
  60. foreach (var bot in bots)
  61. {
  62. var time = DateTime.Now;
  63. if (!bot.IsStarted || !bot.IsRunning || !bot.Diablo.IsRunning || !bot.Demonbuddy.IsRunning)
  64. continue;
  65. if (bot.Diablo.Proc.MainWindowHandle == hwnd)
  66. continue;
  67.  
  68. _lastDiablo = bot.Diablo.MainWindowHandle;
  69. _lastDemonbuddy = bot.Demonbuddy.MainWindowHandle;
  70. Logger.Instance.WriteGlobal("<{0}> Diablo:{1}: lost focus. Bring it back", bot.Name, bot.Diablo.Proc.Id);
  71. // Bring d3 to front
  72. WinAPI.ShowWindow(_lastDiablo, WinAPI.WindowShowStyle.ShowNormal);
  73. WinAPI.SetForegroundWindow(_lastDiablo);
  74.  
  75. // calculate sleeptime
  76. var sleep = (int)(Program.Sleeptime - DateTime.Now.Subtract(time).TotalMilliseconds);
  77. if (sleep > 0) Thread.Sleep(sleep);
  78. }
  79. }
  80. Thread.Sleep(10000);
  81. }
  82. }
  83. catch
  84. {
  85. Thread.Sleep(10000);
  86. ForegroundCheckerWorker();
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment