diff options
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs index 0693b0931..f12678054 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.WatchDog/MainWindowVM.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -16,6 +17,9 @@ namespace Tango.PPC.WatchDog { public class MainWindowVM : ViewModel { + [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] + static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); + private const String tango_process_name = "Tango.PPC.UI"; private WatchDogClient _watchdog; public TextController Log { get; set; } @@ -109,16 +113,16 @@ namespace Tango.PPC.WatchDog { var process = Process.Start(Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), tango_process_name + ".exe")); LogManager.Log("PPC application started."); - LogManager.Log("Waiting for the application to enter an idle state...."); - if (process.WaitForInputIdle((int)TimeSpan.FromMinutes(1).TotalMilliseconds)) + LogManager.Log("Waiting for the application main window...."); + if (WaitForPPCWindow(TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(1))) { - LogManager.Log("Application is in idle state. waiting another 10 seconds..."); + LogManager.Log("Application main window opened. waiting another 10 seconds..."); Thread.Sleep(TimeSpan.FromSeconds(10)); _watchdog.Start(); } else { - LogManager.Log("The PPC application did not enter to an idle state within 1 minute. The Watchdog will not start again!"); + LogManager.Log("The PPC application main window has failed to open within 1 minute. The Watchdog will not start again!"); } } catch (Exception ex) @@ -126,5 +130,21 @@ namespace Tango.PPC.WatchDog LogManager.Log(ex, "Error starting PPC application."); } } + + private bool WaitForPPCWindow(TimeSpan interval, TimeSpan timeout) + { + for (TimeSpan time = TimeSpan.Zero; time < timeout; time = time.Add(interval)) + { + IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Tango - PPC"); + if (windowPtr != IntPtr.Zero) + { + return true; + } + + Thread.Sleep(interval); + } + + return false; + } } } |
