1

On Windows 7 & 10, the Spotify app doesn't prevent the display from turning off or the system going into Sleep mode (at least on the 3 windows machines I'm using). Is there a way to launch the app and incorporate the SetThreadExecutionState function into that thread to prevent the display sleeping while the app is running? Or any other function that will achieve that outcome?

I currently launch and close the app with two separate .bat files that change the sleep timers, but this is pretty clunky so I'd prefer a proper application to do it.

buzz
  • 35
  • 4
  • Fairly straightforward in C: call SetThreadExecutionState, launch Spotify, wait for Spotify to exit, then exit yourself. I imagine the same approach would work in C#. – Harry Johnston Aug 10 '15 at 00:53
  • Thanks for your response. I'm not wedded to using any particular language, what would that look like in C? – buzz Aug 10 '15 at 02:56

1 Answers1

0

This c# solution doesn't use SetThreadExecutionState function, but you mentioned you already have .bat files to change the sleep timers, so you can copy the commands from them into here. C# console application:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var spotify = new Process();
            spotify.StartInfo.FileName = "Spotify.exe";
            spotify.StartInfo.Arguments = "-v -s -a";
            Process.Start("powercfg", "-CHANGE -monitor-timeout-ac 0");
            spotify.Start();
            spotify.WaitForExit();
            var exitCode = spotify.ExitCode;
            spotify.Close();
            Process.Start("powercfg", "-CHANGE -monitor-timeout-ac 1");
        }
    }
}

Put more Process.Start lines to alter hibernate, etc.

Kellamity
  • 16
  • 1
  • 1
    That solution works for both Win 7 and 10 as tested. I substituted the full filepath at `spotify.StartInfo.FileName = "Spotify.exe";` to be `spotify.StartInfo.FileName = "C:\\Mypath\\Spotify.exe";` to get it working. I also set it to run as a Windows application so the console doesn't hover in the background, as per @Hans Passant suggestion here: [link](http://stackoverflow.com/questions/2686289/how-to-run-a-net-console-app-in-the-background). Thanks for your help! – buzz Aug 10 '15 at 22:32
  • 1
    The main problem with this approach is that if the program crashes or is terminated, you don't get the default settings back. But if you're only using it on your own system, that probably isn't an issue. – Harry Johnston Aug 11 '15 at 02:06