toastify
toastify copied to clipboard
Keep running Toastify in background when Spotify is closed
Right now, Toastify runs Spotify when it's started and automatically shuts down when Spotify is closed.
I would like for Toastify to instead continue running in the background, periodically checking if Spotify has been started again. This could potentially be a setting that could be toggled on and off.
Implementing this could probably also allow running Toastify on boot without starting Spotify at the same time.
Hi @kimsey0!
Sounds like a complete waste of computing power to me. What's your use case exactly? Convince me.
The only use cases I can think of for people to want to use Toastify are:
- they want to listen to some music with Spotify, so
- they launch Spotify;
- they later decide they want to be able to use hotkeys or they want a useful toast notificatio only, so they launch Toastify.
- they launch Toastify, which takes care of launching Spotify.
- they launch Spotify;
Hi @aleab! I'd be happy to explain my use case:
I have Spotify pinned to my taskbar, click the icon to run it, and exit it whenever I don't want to listen to music (so I can't accidentally start it). I only use Toastify for the hotkeys, which I wish were built into Spotify. Having to pin another icon to the taskbar and use that to launch Spotify is a nuisance which I'd rather avoid. In general, I just want for Toastify to always run in the background, get out of the way as much as possible, and make it feel like Spotify always had global hotkeys.
Hi @kimsey0. I see, that seems kinda reasonable. The taskbar might get a bit crowded at times and pinning Toastify instead of Spotify wouldn't solve anything. I think I can add that option, but it probably won't happen soon (it won't be in the next release, at least).
I've had a look at the Spotify
class myself, and it doesn't seem like this would be too complex to implement. I can't find a way to hook Windows process creation without elevated privileges, so I'm thinking Toastify may have to run a background thread like spotifyLauncher
and check for a Spotify process every once in a while.
I'm unsure what would be a good trade-off between not using too many system resources and starting to show toasts quickly once Spotify is started.
I honestly know pretty much nothing about partial/full trust code and unelevated/elevated privileges in .NET. What I know, though, is that a .NET application that requires full trust doesn't necessarily require to be run as administrator.
That said, I've found this WMI class that could be used to monitor process creation. Regarding its usage in .NET, the documentation states it requires full trust:
Full trust for the immediate caller. This member cannot be used by partially trusted code.
I didn't yet test whether it works under a non-admin user account, and besides that "full trust" note I haven't found anything in the documentation that states otherwise. I'll test it tomorrow, probably.
EDIT: I found this SO question. One of the answers quotes an MSDN blog entry (source):
By default, unhosted applications are not subject to managed security policy when run under v4.0. Effectively, this means any managed application that you launch from the command prompt or by double clicking the .exe in Windows Explorer will run fully trusted, as will all of the assemblies that it loads (including assemblies that it loads from a location other than the the directory where the executable lives).
So I guess we could theoretically be able to use that WMI class, unless I missed something in the documetation.
I also found the same class, and got an error when I tested it in LINQPad, which made me assume that it required elevated priviledges (run as administrator), but as you noted, it only requires full trust, which Toastify has. That makes it seem like a good solution.
A simple query a la this seems to work great:
string query = "SELECT * FROM __InstanceCreationEvent WITHIN 1 " +
"WHERE TargetInstance isa 'Win32_Process' " +
"AND TargetInstance.Name='Spotify.exe'";
ManagementEventWatcher watcher =
new ManagementEventWatcher(new EventQuery(query));
Then we're golden!! Have you tried running it from a non-admin account as well?
I'll double check myself if it works in the following days and then implement it in the Spotify
class afterwards.
Yes. It works from a non-admin account as well. 😄
Sounds good if you'll implement it. I'll just hold off for now, then.
I've got bad news unfortunately: this method requires admin privileges, unfortunately! 😞
As soon as I called watcher.Start()
to subscribe to the watcher's events, it threw an exception saying "Access denied". It didn't when I run it as admin.
Do you think that requiring admin privileges only to use this feature is a problem? Is it for you, for example? I really want to avoid using a long-running background thread. :worried:
Just throwing in a thought: What about using Toastify as a service? They are meant for such use cases. Couldn't this be set up via the installer as well? Because, doesn't the setup get admin privileges anyway?
But could also be optional, for when the user explicitly wants this functionality. In this case you can inform him/her about the (possible) need for admin rights.
@Vankog How would you set it up? I mean, AFAIK Windows services don't have a GUI, so Toastify service should be separated from the main process and the two should be able to communicate.
Let's say the user decides to "Keep Toastify running in background when Spotify is closed" during installation (and thus install and enable the service, which is indeed possible from the installer), but later decides that they want to disable that option from inside Toastify's Settings window. Is it possible to stop and disable a service without admin privileges? AFAIK it isn't.
I have no experience with Services. But using a service is nothing unusual for some applications. See the several update services of Adobe, Dropbox, Google etc., or IPSEC, IKE daemons of my VPN client or Steam Client Service ("C:\Program Files (x86)\Common Files\Steam\SteamService.exe" /RunAsService) and so on.
but later decides that they want to disable that option from inside Toastify's Settings window. Is it possible to stop and disable a service without admin privileges?
I'm not sure. But having a service doesn't mean you have to use it. There must be a way for a program to manage its own services.
I'll have to take a serious look at services then.
Just had a look at your commit and an idea popped into my mind, because you are disabling the Hotkeys if Spotify is not running: If Spotify is not running, the "Show Spotify" Hotkey might actually be used to start it up.
That's a nice idea! 👍
Any update regarding this?