Skip to content

Commit

Permalink
improved threading
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Jul 1, 2021
1 parent 91f31c9 commit 5fc3584
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public partial class MainWindow : Window
private static GameController _gameController;
private static InputSimulator _inputSimulator;
private static ControllerSettings _settings;

private static bool IsAlive;

[DllImport("User32.Dll")]
public static extern long SetCursorPos(int x, int y);

Expand Down Expand Up @@ -190,7 +191,7 @@ private static int CheckHealth()
public static void ThreadHealth()
{
int health_known = 100;
while (Thread.CurrentThread.IsAlive)
while (IsAlive)
{
if (_gameController.IsConnected())
{
Expand All @@ -213,7 +214,7 @@ public static void ThreadHealth()

public static void ThreadGamepad()
{
while (Thread.CurrentThread.IsAlive)
while (IsAlive)
{
if (_gameController.IsConnected())
_gameController.Poll();
Expand All @@ -224,8 +225,12 @@ public static void ThreadGamepad()

public static void ThreadUI()
{
while (Thread.CurrentThread.IsAlive)
while (IsAlive)
{
// avoid crash on closing
if (Application.Current == null)
return;

_settings.UpdateScreenValues();

if (_settings.hasChanged)
Expand Down Expand Up @@ -323,6 +328,7 @@ public ImageSource ImageSourceFromBitmap(Bitmap bmp)
public MainWindow()
{
InitializeComponent();
IsAlive = true;

// TOUCH SETTINGS
TouchInjector.InitializeTouchInjection();
Expand Down Expand Up @@ -373,6 +379,13 @@ public MainWindow()
myThread3.Start();
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
IsAlive = false;
Application.Current.Shutdown();
}

[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
Expand Down

0 comments on commit 5fc3584

Please sign in to comment.