Guest User

Untitled

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