Skip to content

Commit

Permalink
Delay WarframeProcessFinder timer start until after initialization is…
Browse files Browse the repository at this point in the history
… done, add standardized way to register a post-initialization callback on Main (#330)
  • Loading branch information
D1firehail authored Nov 14, 2024
1 parent 6c8a464 commit d4c5c7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
30 changes: 29 additions & 1 deletion WFInfo/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -529,10 +532,35 @@ public void LoggedIn()
}, null, startTimeSpan, periodTimeSpan);
}

/// <summary>
/// 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
/// </summary>
/// <param name="onLoading">The callback</param>
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)
{
Expand Down
3 changes: 2 additions & 1 deletion WFInfo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public MainWindow()
}

Application.Current.MainWindow = this;
Main.RegisterOnFinishedLoading(FinishedLoading);
}


Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 12 additions & 3 deletions WFInfo/Services/WarframeProcess/WarframeProcessFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d4c5c7b

Please sign in to comment.