Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- using YetAnotherRelogger.Helpers;
- using YetAnotherRelogger.Helpers.Tools;
- namespace YetAnotherRelogger
- {
- public sealed class ForegroundChecker
- {
- #region singleton
- static readonly ForegroundChecker instance = new ForegroundChecker();
- static ForegroundChecker()
- {
- }
- ForegroundChecker()
- {
- }
- public static ForegroundChecker Instance
- {
- get
- {
- return instance;
- }
- }
- #endregion
- private Thread _fcThread;
- public void Start()
- {
- if (_fcThread != null)
- _fcThread.Abort();
- _fcThread = new Thread(new ThreadStart(ForegroundCheckerWorker)) { IsBackground = true };
- _fcThread.Start();
- }
- public void Stop()
- {
- _fcThread.Abort();
- }
- private IntPtr _lastDiablo;
- private IntPtr _lastDemonbuddy;
- private void ForegroundCheckerWorker()
- {
- try
- {
- while (true)
- {
- var bots = BotSettings.Instance.Bots;
- var hwnd = WinAPI.GetForegroundWindow();
- if (_lastDiablo != hwnd)
- {
- _lastDemonbuddy = _lastDiablo = IntPtr.Zero;
- foreach (var bot in bots)
- {
- var time = DateTime.Now;
- if (!bot.IsStarted || !bot.IsRunning || !bot.Diablo.IsRunning || !bot.Demonbuddy.IsRunning)
- continue;
- _lastDiablo = bot.Diablo.MainWindowHandle;
- _lastDemonbuddy = bot.Demonbuddy.MainWindowHandle;
- Logger.Instance.WriteGlobal("<{0}> Diablo:{1}: lost focus. Bring attached it to front", bot.Name, bot.Diablo.Proc.Id);
- WinAPI.ShowWindow(hwnd, WinAPI.WindowShowStyle.Minimize);
- Thread.Sleep(500);
- WinAPI.ShowWindow(_lastDiablo, WinAPI.WindowShowStyle.Minimize);
- Thread.Sleep(500);
- WinAPI.ShowWindow(_lastDiablo, WinAPI.WindowShowStyle.ShowNormal);
- Thread.Sleep(500);
- WinAPI.SetForegroundWindow(_lastDiablo);
- // calculate sleeptime
- var sleep = (int)(Program.Sleeptime - DateTime.Now.Subtract(time).TotalMilliseconds);
- if (sleep > 0) Thread.Sleep(sleep);
- }
- }
- Thread.Sleep(15000);
- }
- }
- catch
- {
- Thread.Sleep(15000);
- ForegroundCheckerWorker();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment