Skip to content

Commit

Permalink
Change auto-start and auto-shutdown behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdude123 committed Feb 15, 2021
1 parent f856f1d commit ad0408e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
9 changes: 0 additions & 9 deletions GroovyRPCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@ private static void Main()
TrackInfo oldTrack = new TrackInfo();

bool isPresenceActive = false;
bool hasGrooveMusicStartedOnce = false;

while (_client.IsInitialized)
{
Process[] grooveMusics = Process.GetProcessesByName("Music.UI");
if (hasGrooveMusicStartedOnce && grooveMusics.Length < 1)
{
Environment.Exit(0);
} else if (grooveMusics.Length > 0)
{
hasGrooveMusicStartedOnce = true;
}
if (_grooveInfoFetcher.IsUsingAudio())
{
try
Expand Down
56 changes: 36 additions & 20 deletions GroovyRPHelper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ static void Main()
}

Config myConfig = LoadConfiguration();
if(myConfig.RunWhenGrooveMusicOpens)
{
Process[] grooveMusicInstances = Process.GetProcessesByName("Music.UI");
if (grooveMusicInstances.Length > 0)
{
Process grooveMusic = grooveMusicInstances[0];
if ((grooveMusic.Threads[0].ThreadState == ThreadState.Wait) && grooveMusic.Threads[0].WaitReason == ThreadWaitReason.Suspended)
{
// Some times Groove Music will start up suspended in the background with Suspended state and generate an event.
// Ok to close GroovyRP since user did not open Groove Music.
Environment.Exit(0);
}
}
}

string corePath = Path.GetDirectoryName(Application.ExecutablePath) + @"\GroovyRPCore.exe";
if (myConfig.HideInSystemTray)
Expand Down Expand Up @@ -88,10 +74,11 @@ static void Main()
systemTray.ContextMenu = contextMenu;
systemTray.Visible = true;

if(myConfig.AutoCheckForUpdates) UpdateCheck();
if(myConfig.AutoCheckForUpdates) UpdateCheck(null, null);

while (!coreProcess.HasExited)
{
checkGrooveMusicStatus(); // Don't allow program to keep running if Groove Music isn't running
Application.DoEvents();
}
Environment.Exit(0);
Expand Down Expand Up @@ -223,11 +210,6 @@ private static void Reconfigure(object Sender, EventArgs e)
}

private static void UpdateCheck(object Sender, EventArgs e)
{
UpdateCheck();
}

private static void UpdateCheck()
{
try
{
Expand All @@ -251,6 +233,40 @@ private static void UpdateCheck()
catch { }
}

public static void checkGrooveMusicStatus()
{
Process[] grooveMusics = Process.GetProcessesByName("Music.UI");
if (grooveMusics.Length < 1)
{
Environment.Exit(0);
}
else if (grooveMusics.Length > 0)
{
try
{
Process grooveMusic = grooveMusics[0];
ProcessThread grooveMusicMainThread = null;

for (int i = 0; i < grooveMusic.Threads.Count; i++)
{
if (grooveMusicMainThread == null || grooveMusic.Threads[i].StartTime < grooveMusicMainThread.StartTime)
{
grooveMusicMainThread = grooveMusic.Threads[i];
}
}

if (grooveMusicMainThread.ThreadState.Equals(ThreadState.Terminated) ||
(grooveMusicMainThread.ThreadState.Equals(ThreadState.Wait) && grooveMusicMainThread.WaitReason.Equals(ThreadWaitReason.Suspended)))
{
ExitProgram(null, null);
}
} catch (InvalidOperationException)
{

}
}
}

private static void UpdateNotificationClicked(object Sender, EventArgs e)
{
Process.Start(@"https://github.com/dsdude123/GroovyRP/releases/latest");
Expand Down

0 comments on commit ad0408e

Please sign in to comment.