WhenPowerEvent does not react to SystemEvents.PowerModeChanged #384
Comments
Im having the same problem class Program
{
public static void Log(string message)
{
var content = DateTime.Now.ToString("G") + " " + message + Environment.NewLine;
Console.Write(content);
System.IO.File.AppendAllText("Log.log", content);
}
static void Main(string[] args)
{
var rc = HostFactory.Run(x =>
{
//Setup topshelf logging
x.Service<Service>(s =>
{
s.ConstructUsing(name => new Service());
s.WhenStarted(sw => sw.Start());
s.WhenStopped(sw => sw.Stop());
s.WhenPowerEvent((sw, host, arg) => {
Log($"Power event {arg.EventCode}");
return true;
});
});
x.RunAsLocalSystem();
x.StartAutomatically();
x.SetStopTimeout(TimeSpan.FromSeconds(20));
x.EnableServiceRecovery(r =>
{
r.OnCrashOnly();
r.RestartService(0);
r.SetResetPeriod(1);
});
x.AfterUninstall(() =>
{
Log("AfterUninstall");
});
});
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
Environment.ExitCode = exitCode;
}
}
class Service
{
public void Start()
{
Program.Log("Start");
}
public void Stop()
{
Program.Log("Stop");
}
} No Power event is logged, what am i doing wrong? (Going with the workaround until this is fixed) |
I haven't used Topshelf and came across this issue while looking for PowerModeChanged, but I think I know why it isn't working. The service needs a message pump in order to process system events. Here are a couple of links: |
I'm wondering why
And I can't find a place in the codebase where I can set this to true.
See also #170 |
When the computer goes to Standby, the
WhenPowerEvent
does not react. Also, I might want to have a reference to the service in order to start and stop it on standby.Workaround
The text was updated successfully, but these errors were encountered: