diff --git a/WFInfo/Main.cs b/WFInfo/Main.cs index e318b56d..0b10a1c4 100644 --- a/WFInfo/Main.cs +++ b/WFInfo/Main.cs @@ -48,6 +48,9 @@ class Main public static PlusOne plusOne = new PlusOne(); public static System.Threading.Timer timer; public static System.Drawing.Point lastClick; + + public static bool Initialized; + private static event EventHandler onInitialized; private const int minutesTillAfk = 7; private static bool UserAway { get; set; } @@ -529,10 +532,35 @@ public void LoggedIn() }, null, startTimeSpan, periodTimeSpan); } + /// + /// Register a callback to be ran once initialization (including data load) is complete. + /// If initialization is already done, callback gets invoked immediately + /// The callback is always ran on the GUI thread + /// + /// The callback + public static void RegisterOnFinishedLoading(EventHandler onLoading) + { + System.Windows.Application.Current.Dispatcher.InvokeAsync(() => + { + if (Initialized) + { + onLoading(null, EventArgs.Empty); + } + else + { + onInitialized += onLoading; + } + }); + } + public static void FinishedLoading() { - MainWindow.INSTANCE.Dispatcher.Invoke(() => { MainWindow.INSTANCE.FinishedLoading(); }); + System.Windows.Application.Current.Dispatcher.Invoke(() => + { + Initialized = true; + onInitialized?.Invoke(null, EventArgs.Empty); + }); } public static void UpdateMarketStatus(string msg) { diff --git a/WFInfo/MainWindow.xaml.cs b/WFInfo/MainWindow.xaml.cs index 172aae82..fc9adf57 100644 --- a/WFInfo/MainWindow.xaml.cs +++ b/WFInfo/MainWindow.xaml.cs @@ -71,6 +71,7 @@ public MainWindow() } Application.Current.MainWindow = this; + Main.RegisterOnFinishedLoading(FinishedLoading); } @@ -354,7 +355,7 @@ internal void LoggOut(object sender, CancelEventArgs e) Task.Factory.StartNew(() => { Main.dataBase.Disconnect(); }); } - internal void FinishedLoading() + internal void FinishedLoading(object sender, EventArgs e) { Login.IsEnabled = true; } diff --git a/WFInfo/Services/WarframeProcess/WarframeProcessFinder.cs b/WFInfo/Services/WarframeProcess/WarframeProcessFinder.cs index 417ccb37..40f2ae24 100644 --- a/WFInfo/Services/WarframeProcess/WarframeProcessFinder.cs +++ b/WFInfo/Services/WarframeProcess/WarframeProcessFinder.cs @@ -3,7 +3,7 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using WFInfo.Settings; -using System.Windows.Threading; +using System.Threading; namespace WFInfo.Services.WarframeProcess { @@ -67,12 +67,21 @@ private set private Process _warframe; private readonly IReadOnlyApplicationSettings _settings; - private System.Threading.Timer find_process_timer; + private Timer find_process_timer; + private const int FindProcessTimerDuration = 40000; // ms public WarframeProcessFinder(IReadOnlyApplicationSettings settings) { _settings = settings; - find_process_timer = new System.Threading.Timer(FindProcess, null, 0, 40000); + // create timer, but don't start yet. Some static fields may not be ready yet + find_process_timer = new Timer(FindProcess, null, Timeout.Infinite, FindProcessTimerDuration); + Main.RegisterOnFinishedLoading(Main_OnInitialized); + } + + private void Main_OnInitialized(object sender, EventArgs e) + { + // main is Initialized, start timer + find_process_timer.Change(0, FindProcessTimerDuration); } private void FindProcess(Object stateInfo)